Skip to content

Commit 72d650f

Browse files
authored
Rollup merge of #106453 - coastalwhite:master, r=GuillaumeGomez
Improve include macro documentation As outlined in #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
2 parents 498216e + ae667be commit 72d650f

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

library/core/src/macros/mod.rs

+31-12
Original file line numberDiff line numberDiff line change
@@ -1315,22 +1315,41 @@ pub(crate) mod builtin {
13151315

13161316
/// Parses a file as an expression or an item according to the context.
13171317
///
1318-
/// The file is located relative to the current file (similarly to how
1319-
/// modules are found). The provided path is interpreted in a platform-specific
1320-
/// way at compile time. So, for instance, an invocation with a Windows path
1321-
/// containing backslashes `\` would not compile correctly on Unix.
1318+
/// **Warning**: For multi-file Rust projects, the `include!` macro is probably not what you
1319+
/// are looking for. Usually, multi-file Rust projects use
1320+
/// [modules](https://doc.rust-lang.org/reference/items/modules.html). Multi-file projects and
1321+
/// modules are explained in the Rust-by-Example book
1322+
/// [here](https://doc.rust-lang.org/rust-by-example/mod/split.html) and the module system is
1323+
/// explained in the Rust Book
1324+
/// [here](https://doc.rust-lang.org/book/ch07-02-defining-modules-to-control-scope-and-privacy.html).
1325+
///
1326+
/// The included file is placed in the surrounding code
1327+
/// [unhygienically](https://doc.rust-lang.org/reference/macros-by-example.html#hygiene). If
1328+
/// the included file is parsed as an expression and variables or functions share names across
1329+
/// both files, it could result in variables or functions being different from what the
1330+
/// included file expected.
1331+
///
1332+
/// The included file is located relative to the current file (similarly to how modules are
1333+
/// found). The provided path is interpreted in a platform-specific way at compile time. So,
1334+
/// for instance, an invocation with a Windows path containing backslashes `\` would not
1335+
/// compile correctly on Unix.
13221336
///
1323-
/// Using this macro is often a bad idea, because if the file is
1324-
/// parsed as an expression, it is going to be placed in the
1325-
/// surrounding code unhygienically. This could result in variables
1326-
/// or functions being different from what the file expected if
1327-
/// there are variables or functions that have the same name in
1328-
/// the current file.
1337+
/// # Uses
1338+
///
1339+
/// The `include!` macro is primarily used for two purposes. It is used to include
1340+
/// documentation that is written in a separate file and it is used to include [build artifacts
1341+
/// usually as a result from the `build.rs`
1342+
/// script](https://doc.rust-lang.org/cargo/reference/build-scripts.html#outputs-of-the-build-script).
1343+
///
1344+
/// When using the `include` macro to include stretches of documentation, remember that the
1345+
/// included file still needs to be a valid rust syntax. It is also possible to
1346+
/// use the [`include_str`] macro as `#![doc = include_str!("...")]` (at the module level) or
1347+
/// `#[doc = include_str!("...")]` (at the item level) to include documentation from a plain
1348+
/// text or markdown file.
13291349
///
13301350
/// # Examples
13311351
///
1332-
/// Assume there are two files in the same directory with the following
1333-
/// contents:
1352+
/// Assume there are two files in the same directory with the following contents:
13341353
///
13351354
/// File 'monkeys.in':
13361355
///

0 commit comments

Comments
 (0)