rustc:参数必须是字符串文字

rustc: argument must be a string literal

我有 test.rs:

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

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

rustc test.rs

如何解决这个错误?

error: argument must be a string literal

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

宏需要一个字符串文字,所以它不能是一个变量:

include_bytes!("Test.bin");

或者,您也可以创建一个宏来扩展为所需的值:

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

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

Playground