Home > Blockchain >  rustc: argument must be a string literal
rustc: argument must be a string literal

Time:10-06

I have test.rs:

const TEST: &'static str = "Test.bin";

fn main() {
    let x = include_bytes!(TEST);
}

rustc test.rs

How to fix this error?

error: argument must be a string literal

 --> test.rs:4:22
  |
4 |     let x = include_bytes!(TEST);
  |                      ^^^^

CodePudding user response:

The macro expects a string literal, so it cannot be a variable:

include_bytes!("Test.bin");

Alternatively you could create a macro that will expand into the desired value too:

macro_rules! test_bin {
    // `()` indicates that the macro takes no argument.
    () => {
        "Test.bin"
    };
}

fn main() {
    let x = include_bytes!(test_bin!());
}

Playground

  • Related