Include_bytes!() not working how I thought it works

⚓ Rust    📅 2026-01-29    👤 surdeus    👁️ 10      

surdeus

Warning

This post was published 33 days ago. The information described in this article may have changed.

Continuing to work on the same program and I needed to find a solution to include a pre-existing binary in my Rust application. Similar to embedded resources in C#. Enter, include_bytes!() macro. I think I might be misunderstanding it's purpose or maybe it's the wrong tool for what I'm trying to do.

I have a function:

fn write_arti_bin(){
    let arti_bin = include_bytes!("C:\\Code\\rust\\src\\bins\\arti.exe");
    let filename= "arti.exe";
    let cur_dir = env::current_dir().unwrap();
    let full_path = PathBuf::from(cur_dir).join(filename);

    fs::write(full_path, arti_bin).expect("Unable to write file.");
}

This works just fine. When I compile the program and run it on my computer, it works fine. Thinking about it now, that makes sense that it would because my source arti.exe is still present at that absolute path...

I thought what would happen is that after compiling, the bytes would be in the output binary of my rust application. That does seem to be the case because when I run my binary on another computer, the arti.exe file is written to disk and is functional. BUT it errors out and panics because it can't find the source file included in let arti_bin = [...] line so it never executes the next step of the program. Again, this makes sense now because of course it couldn't find the source since it's a different machine. Although I am a bit confused why it writes to file at all if the source line happens before the write line but that's tangential to my question I think.

Since the bytes have already been read in when the program was compiled, it doesn't need to read from source anymore, it can just write the file to disk.

So my question is how do I achieve that?
Is there a decoration I can put that says to ignore that line if not running in debug mode or something?
Is include_bytes!() not the way to go for what I'm trying to do?

Thank you for your help as always!

13 posts - 5 participants

Read full topic

🏷️ Rust_feed