macro

include_str! — Embed a UTF-8 text file

Embed the contents of a UTF-8 file during compilation, including whitespace and final newlines. The file is not reopened when the executable runs. A missing file or invalid UTF-8 prevents compilation; use runtime file reading for changeable content. Result: &'static str. Fixture prerequisites: greeting.txt (see catalog for exact contents). Expected output (not executed): 'Welcome!\nBytes: 9\n' Common mistake: Editing the file and expecting an existing executable to reflect the change.

Formal Syntax & Structure

rust syntaxrust
include_str!("relative/path.txt")

Working Example

Example Snippetrust
fn main() {    let message: &'static str = include_str!("greeting.txt");    print!("{message}");    println!("Bytes: {}", message.len());}
Rust Reference