Disclaimer: I mentioned this in a comment, but let me rename it here as it gives me more development options.
As Shepmaster said, you can include text or a binary file in the Rust library / executable using include_bytes! macros include_bytes! and include_str! .
In your case, however, I would avoid this. Putting off parsing content for runtime:
- You allow the creation of a defective artifact.
- you bear (more) overhead at runtime (parsing time).
- you bear (more) overhead (parsing code).
Rust confirms this problem and offers several code generation mechanisms designed to overcome these limitations:
- macros: if the logic can be encoded into a macro, then it can be directly included in the source file
- plugins: activated macros that can encode any arbitrary logic and generate complex code (see
regex! ) build.rs : an independent "Rust script" that runs ahead of the compilation itself, the role of which is to generate .rs files
In your case, the build.rs script sounds good:
- by moving the parsing code there, you get a lighter artifact
- using parsing ahead of time, you get a faster artifact
- by parsing in advance, you deliver the correct artifact
The result of your analysis can be encoded in different ways: from functions to statics (possibly lazy_static! ), Because build.rs can generate any valid Rust code.
You can see how to use build.rs in the build.rs Documentation ; You will find there how to integrate it with Cargo and how to create files (and much more).
source share