42

This is the cousin of this question over here asking the same thing for C.

Basically, is there a better way than to just turn it into a giant byte array and putting it in a source file?

Alternatively, does a macro have the ability to do this? (Rust Macros... are a dense looking and their exact capabilities are not known to me.)

1

2 Answers 2

49

You probably want include_bytes!.

If you are in older versions of Rust, use include_bin! instead.

Sign up to request clarification or add additional context in comments.

2 Comments

Perfect. :) I had a feeling a macro would do it, but google didn't come up with anything and I just didn't think to search there :)
Note that this macro has been renamed to include_bytes! in newer Rust versions.
9

You could alternatively use this tool https://github.com/pyros2097/rust-embed Which was created by me which generates rust code for your resources.

2 Comments

Would you recommend using this for a 50 MB binary file which I plan to parse with nom? Thanks
Yeah go for it. The only thing that I could see that be a problem is in release mode the macro will generate a Vec<u8> which is going to have 50MB of code generated. I don't know how long rust will take to compile the code and whether the code will get generated properly in the first place since I haven't tested with such a large file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.