3

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);
  |                      ^^^^
  • How to fix ? The answer is in the question: put a string literal between the parenthesis. `let x = include_bytes!("test.bin");` – Denys Séguret Oct 05 '21 at 12:31
  • I need to take this value out of the function to make interaction with the code easier –  Oct 05 '21 at 12:32
  • Then you can't use this macro. Do you really expect to include in the binary something dynamic ? – Denys Séguret Oct 05 '21 at 12:32
  • 1
    Wait let me get this right, this macro can't even accept a STATIC string parameter? What kind of useless bs is this then? I mean, it is very bad practice to hardcode filenames inside functions, so who thought this would be a good idea? – Spectraljump Mar 14 '22 at 19:20

1 Answers1

4

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

Netwave
  • 40,134
  • 6
  • 50
  • 93