Loading byte arrays and strings at compile time

You can load various types of constants at compile time. The include_bytes!() macro will create a byte (u8) array with the contents of the specified file. The include_str!() macro, on the other hand, will get the contents of the file as a string and create a &static str. Both will make the compilation fail if the file does not exist at compile time.

You can also use the include!() macro that will include the code of the given file and add it to the current file at compile time. The compilation will also fail if the code in that file is not valid Rust code:

const CRATE_CONFIG: &str = include_str!("../Cargo.toml");

Get Rust High Performance now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.