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();
newpath.push_str(&path);
newpath.push_str(&png_vec[0]);
let black_rook_image =
RenderBuffer::decode_from_bytes(include_bytes!(
newpath)).unwrap();
Error is:
error: argument must be a string literal
--> src/cot_app.rs:76:13
|
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.
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.