Improve include macro documentation

This commit is contained in:
Gijs Burghoorn 2023-01-04 16:07:33 +01:00
parent 5c18bc6137
commit 0c43b42b0c

View File

@ -1312,28 +1312,52 @@ macro_rules! cfg {
/* compiler built-in */ /* compiler built-in */
}; };
} }
/// Parses a file as an expression or an item according to the context. /// Parses a file as an expression or an item according to the context.
/// ///
/// The file is located relative to the current file (similarly to how /// <div class="example-wrap" style="display:inline-block">
/// modules are found). The provided path is interpreted in a platform-specific /// <pre class="compile_fail" style="white-space:normal;font:inherit;">
/// way at compile time. So, for instance, an invocation with a Windows path
/// containing backslashes `\` would not compile correctly on Unix.
/// ///
/// Using this macro is often a bad idea, because if the file is /// **Warning**: For multi-file Rust projects, the `include!` macro is probably not what you
/// parsed as an expression, it is going to be placed in the /// are looking for. Usually, multi-file Rust projects use
/// surrounding code unhygienically. This could result in variables /// [modules](https://doc.rust-lang.org/reference/items/modules.html). Multi-file projects and
/// or functions being different from what the file expected if /// modules are explained in the Rust-by-Example book
/// there are variables or functions that have the same name in /// [here](https://doc.rust-lang.org/rust-by-example/mod/split.html) and the module system is
/// the current file. /// explained in the Rust Book
/// [here](https://doc.rust-lang.org/book/ch07-02-defining-modules-to-control-scope-and-privacy.html).
/// ///
/// </pre>
/// </div>
///
/// If the included file is parsed as an expression, it is placed in the surrounding code
/// [unhygienically](https://doc.rust-lang.org/reference/macros-by-example.html#hygiene). 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 included 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. It is used to include
/// documentation that is written in a separate file and it is used to include [build artifacts
/// usually as a result from the `build.rs`
/// script](https://doc.rust-lang.org/cargo/reference/build-scripts.html#outputs-of-the-build-script).
///
/// When using the `include` macro to include stretches of documentation, remember that the
/// included file still needs to be a valid rust syntax. It is also possible to
/// use the [`include_str`] macro as `#![doc = include_str!("...")]` (at the module level) or
/// `#[doc = include_str!("...")]` (at the item level) to include documentation from a plain
/// text or markdown file.
///
/// # Examples /// # Examples
/// ///
/// Assume there are two files in the same directory with the following /// Assume there are two files in the same directory with the following contents:
/// contents: ///
///
/// File 'monkeys.in': /// File 'monkeys.in':
/// ///
/// ```ignore (only-for-syntax-highlight) /// ```ignore (only-for-syntax-highlight)
/// ['🙈', '🙊', '🙉'] /// ['🙈', '🙊', '🙉']
/// .iter() /// .iter()
@ -1341,9 +1365,9 @@ macro_rules! cfg {
/// .take(6) /// .take(6)
/// .collect::<String>() /// .collect::<String>()
/// ``` /// ```
/// ///
/// File 'main.rs': /// File 'main.rs':
/// ///
/// ```ignore (cannot-doctest-external-file-dependency) /// ```ignore (cannot-doctest-external-file-dependency)
/// fn main() { /// fn main() {
/// let my_string = include!("monkeys.in"); /// let my_string = include!("monkeys.in");
@ -1351,7 +1375,7 @@ macro_rules! cfg {
/// println!("{my_string}"); /// println!("{my_string}");
/// } /// }
/// ``` /// ```
/// ///
/// Compiling 'main.rs' and running the resulting binary will print /// Compiling 'main.rs' and running the resulting binary will print
/// "🙈🙊🙉🙈🙊🙉". /// "🙈🙊🙉🙈🙊🙉".
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]