Macro load_file::load_str[][src]

macro_rules! load_str {
    ($name:expr) => { ... };
}

Load a utf8-encoded file as a string at run-time.

The file is located relative to the current source file, and the binary must be run with the crate root as the working directory.

The resulting value is a &'static str with the contents of the file.

This macro can often be a drop-in replacement for include_str!, switching it to be a run-time rather than compile-time operation.

Each time the macro is reached, the file is read into memory in its entirety and the memory is leaked, keeping the memory valid for the remainder of the program execution.

Compatibility with include_str!

Apart from the semantic differences between include_str! and load_str! there are also a technical difference:

include_str! can appear in static contexts in the source code, while load_str! can not. It is possible to use the lazy_static crate to work around this.

Example

#[macro_use]
extern crate load_file;

fn main() {
    let greeting: &str = load_str!("greeting.txt");
    println!("{}", greeting);
}

Panics

To facilitate using load_str! as a drop-in replacement for include_str!, all error situations cause panics:

  • File not found
  • Read errors
  • UTF-8 validation errors