Skip to content

Commit 0c43b42

Browse files
committed
Improve include macro documentation
1 parent 5c18bc6 commit 0c43b42

File tree

1 file changed

+44
-20
lines changed

1 file changed

+44
-20
lines changed

library/core/src/macros/mod.rs

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,46 +1312,70 @@ pub(crate) mod builtin {
13121312
/* compiler built-in */
13131313
};
13141314
}
1315-
1315+
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.
1322-
///
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.
1318+
/// <div class="example-wrap" style="display:inline-block">
1319+
/// <pre class="compile_fail" style="white-space:normal;font:inherit;">
1320+
///
1321+
/// **Warning**: For multi-file Rust projects, the `include!` macro is probably not what you
1322+
/// are looking for. Usually, multi-file Rust projects use
1323+
/// [modules](https://doc.rust-lang.org/reference/items/modules.html). Multi-file projects and
1324+
/// modules are explained in the Rust-by-Example book
1325+
/// [here](https://doc.rust-lang.org/rust-by-example/mod/split.html) and the module system is
1326+
/// explained in the Rust Book
1327+
/// [here](https://doc.rust-lang.org/book/ch07-02-defining-modules-to-control-scope-and-privacy.html).
1328+
///
1329+
/// </pre>
1330+
/// </div>
1331+
///
1332+
/// If the included file is parsed as an expression, it is placed in the surrounding code
1333+
/// [unhygienically](https://doc.rust-lang.org/reference/macros-by-example.html#hygiene). This
1334+
/// could result in variables or functions being different from what the file expected if there
1335+
/// are variables or functions that have the same name in the current file.
1336+
///
1337+
/// The included file is located relative to the current file (similarly to how modules are
1338+
/// found). The provided path is interpreted in a platform-specific way at compile time. So,
1339+
/// for instance, an invocation with a Windows path containing backslashes `\` would not
1340+
/// compile correctly on Unix.
13291341
///
1342+
/// # Uses
1343+
///
1344+
/// The `include!` macro is primarily used for two purposes. It is used to include
1345+
/// documentation that is written in a separate file and it is used to include [build artifacts
1346+
/// usually as a result from the `build.rs`
1347+
/// script](https://doc.rust-lang.org/cargo/reference/build-scripts.html#outputs-of-the-build-script).
1348+
///
1349+
/// When using the `include` macro to include stretches of documentation, remember that the
1350+
/// included file still needs to be a valid rust syntax. It is also possible to
1351+
/// use the [`include_str`] macro as `#![doc = include_str!("...")]` (at the module level) or
1352+
/// `#[doc = include_str!("...")]` (at the item level) to include documentation from a plain
1353+
/// text or markdown file.
1354+
///
13301355
/// # Examples
1331-
///
1332-
/// Assume there are two files in the same directory with the following
1333-
/// contents:
1334-
///
1356+
///
1357+
/// Assume there are two files in the same directory with the following contents:
1358+
///
13351359
/// File 'monkeys.in':
1336-
///
1360+
///
13371361
/// ```ignore (only-for-syntax-highlight)
13381362
/// ['🙈', '🙊', '🙉']
13391363
/// .iter()
13401364
/// .cycle()
13411365
/// .take(6)
13421366
/// .collect::<String>()
13431367
/// ```
1344-
///
1368+
///
13451369
/// File 'main.rs':
1346-
///
1370+
///
13471371
/// ```ignore (cannot-doctest-external-file-dependency)
13481372
/// fn main() {
13491373
/// let my_string = include!("monkeys.in");
13501374
/// assert_eq!("🙈🙊🙉🙈🙊🙉", my_string);
13511375
/// println!("{my_string}");
13521376
/// }
13531377
/// ```
1354-
///
1378+
///
13551379
/// Compiling 'main.rs' and running the resulting binary will print
13561380
/// "🙈🙊🙉🙈🙊🙉".
13571381
#[stable(feature = "rust1", since = "1.0.0")]

0 commit comments

Comments
 (0)