Need to get past string literal requirement

A string literal is required in a crate-supplied function, and I want to give it a var instead of a string literal. Is there a way to "fool" the crate-supplied function into accepting a var as in the following code:

        let png_vec = vec!("BlackRook.png", "WhiteRook.png");
        let path = "/home/nexus/Documents/VSCode_Projects/VSCode_Rust_Projects/cot-ecs-like-sq/assets/";
        let mut newpath = "".to_string();
        let black_rook_image =

Error is:

error: argument must be a string literal
  --> src/
76 |             newpath)).unwrap();
   |             ^^^^^^^

The include_bytes!() macro reads the file at compile time and pack its content at the executable binary itself, just like how string literal contents are packed into the binary. This is why it can't take a runtime-known file path. If you want to read file at runtime, you can use File type instead, or just std::fs::read(&newpath) for convenience.

@Hyeonu Is there any advantage to using one method over the other?

It's not about advantage but possibility. You can't use include_bytes!() macro with runtime constructed path.

Edit: If you're asking the advantage of the include_bytes!() macro, the compiler packs the content into the binary itself so it doesn't perform any filesystem operation on runtime to read the bytes.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.