You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Having talked to some new people who are learning rust, the module system is something people often take some time to understand. People coming from C may search rust include file and find the include! macro. I feel like this should clearly link to the modules documentation.
Currently, there is a small part on include being something that usually a bad idea:
Using this macro is often a bad idea, because if the file is parsed as an expression, it is going to be placed in the surrounding code unhygienically. This could result in variables or functions being different from what the file expected if there are variables or functions that have the same name in the current file.
I feel like this part of the documentation should very directly and plainly reference resources about the module system and how that is what they should probably be using. This can link to one or multiple official resources (e.g. The Reference, Rust-by-Example - Modules: File Hierarchy or The Book). Possibly, we can refer to all these resources, although that might be overwhelming to the reader. I would say to add this after the first line as a sort of warning.
It would also be interesting to add where you might then actually use include! macro similar to the Uses header in the assert macro. From my experience, the include macro has two primary uses:
Including large parts of documentation
Including build artifacts from the OUT_DIR of build scripts
A very primitive draft of the adjusted documentation would be:
Parses a file as an expression or an item according to the context.
For multi-file Rust projects, the include! macro is probably not what you are looking for. Usually, multi-file Rust projects use modules. Multi-file projects and modules are explained in the Rust-by-Example book here and the module system is explained in the Rust Book here.
The included file is parsed as an expression, it is going to be placed in the surrounding code unhygienically. This could result in variables or functions being different from what the file expected if there are variables or functions that have the same name in the current file.
The file is located relative to the current file (similarly to how modules are found). The provided path is interpreted in a platform-specific way at compile time. So, for instance, an invocation with a Windows path containing backslashes \ would not compile correctly on Unix.
Uses
The include! macro is primarily used for two purposes. The macro can be used to include long stretches of documentation. This can also be done with #![doc = include_str!("...")]. The second common use-case for the include! macro is when including the output artifacts of the build script.
Examples
Assume there are two files in the same directory with the following contents:
Improve include macro documentation
As outlined in rust-lang#106118, the `include!` macro is a SEO problem when it comes to the Rust documentation. Beginners may see it as a replacement to `include` syntax in other languages. I feel like this documentation should quite explicitly link to the modules' documentation.
The primary goal of this PR is to address that issue by adding a warning to the documentation. While I was here, I also added some other parts. This included a `Uses` section and some (intra doc) links to other relevant topics.
I hope this can help beginners to Rust more quickly understand some multi-file project intricacies.
# References
- Syntax for the warning: https://github.com/tokio-rs/tracing/blob/58accc6da3f04af3f6144fbe6d68af7225c70c02/tracing/src/lib.rs#L55
Location
include!
macroSummary
Having talked to some new people who are learning rust, the module system is something people often take some time to understand. People coming from
C
may searchrust include file
and find theinclude!
macro. I feel like this should clearly link to the modules documentation.Currently, there is a small part on include being something that usually a bad idea:
I feel like this part of the documentation should very directly and plainly reference resources about the module system and how that is what they should probably be using. This can link to one or multiple official resources (e.g. The Reference, Rust-by-Example - Modules: File Hierarchy or The Book). Possibly, we can refer to all these resources, although that might be overwhelming to the reader. I would say to add this after the first line as a sort of warning.
It would also be interesting to add where you might then actually use
include!
macro similar to theUses
header in the assert macro. From my experience, the include macro has two primary uses:OUT_DIR
of build scriptsA very primitive draft of the adjusted documentation would be:
I can create a PR if that is wanted.
The text was updated successfully, but these errors were encountered: