35

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.)

Community
  • 1
  • 1
Logan
  • 1,614
  • 1
  • 14
  • 27
  • Does this answer your question? [Is there a good way to include external resource data into Rust source code?](https://stackoverflow.com/questions/25505275/is-there-a-good-way-to-include-external-resource-data-into-rust-source-code) – Cristian Ciupitu Jun 25 '20 at 22:46

2 Answers2

42

You probably want include_bytes!.

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

dgilperez
  • 10,716
  • 8
  • 68
  • 96
DK.
  • 55,277
  • 5
  • 189
  • 162
  • 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 :) – Logan Nov 26 '14 at 03:37
  • Note that this macro has been renamed to [`include_bytes!`](http://doc.rust-lang.org/std/macro.include_bytes!.html) in newer Rust versions. – Adrian Willenbücher Feb 12 '15 at 13:07
8

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
    Would you recommend using this for a 50 MB binary file which I plan to parse with `nom`? Thanks – ChrisR Mar 26 '18 at 05:33
  • 2
    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 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. –  Mar 26 '18 at 18:30