W3cubDocs

/Rust

Macro std::include

macro_rules! include {
    ($file:expr) => { ... };
    ($file:expr,) => { ... };
}

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 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.

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.

Examples

Assume there are two files in the same directory with the following contents:

File 'monkeys.in':

β“˜This example is not tested
['πŸ™ˆ', 'πŸ™Š', 'πŸ™‰']
    .iter()
    .cycle()
    .take(6)
    .collect::<String>()

File 'main.rs':

β“˜This example is not tested
fn main() {
    let my_string = include!("monkeys.in");
    assert_eq!("πŸ™ˆπŸ™ŠπŸ™‰πŸ™ˆπŸ™ŠπŸ™‰", my_string);
    println!("{}", my_string);
}

Compiling 'main.rs' and running the resulting binary will print "πŸ™ˆπŸ™ŠπŸ™‰πŸ™ˆπŸ™ŠπŸ™‰".

Β© 2010 The Rust Project Developers
Licensed under the Apache License, Version 2.0 or the MIT license, at your option.
https://doc.rust-lang.org/std/macro.include.html