2 ms·
In Rust you can just use the built in `include_str!()` or `include_bytes!()` macros. let content: &'static str = include_str!("path/to/file");
by nicoburns 5y ago
In Rust you can just use the built in `include_str!()` or `include_bytes!()` macros.
let content: &'static str = include_str!("path/to/file");
- scottlamb 5y agoThe bindata package that samhw linked goes further by walking a whole directory and embedding it all, supplying a function that maps from pathname to file contents. Looks like it returns an Option<Vec<u8>> though [1] which means it must be copying the file contents each time. I don't know why they didn't just return a &'static [u8] instead. There's another crate includedir which looks more popular. It also supports compression. But if the file is stored in compressed form, looks like it also will (decompress/)copy into a fresh Vec, whether you request the uncompressed or compressed forms. [2] That's not what I would like. I'd prefer (lazily or eagerly) decompressing once and keeping it around in RAM for next time. YMMV. I could use a high-quality implementation of this same idea. Personally I don't get using it for a personal blog (I'd rather be able to change the content without recompiling/restarting), but on my todo list is producing a zero-dependency, single-binary form of software I'm working on, including its web interface. I might end up writing my own. [1] https://github.com/glassbearInc/rs-bindata/blob/93f61807b206d246aa920a5c0edf6317e11ebf04/rs-bindata-impl/src/lib.rs#L163 https://github.com/glassbearInc/rs-bindata/blob/93f61807b206... [2] lines 59 and 80, respectively. https://github.com/tilpner/includedir/blob/6a81c906e233649af1acec9835fe0a034b9274e2/lib/src/lib.rs#L59 https://github.com/tilpner/includedir/blob/6a81c906e233649af...
- samhw 5y agoOh nice, thank you! I’d like to pretend that I intentionally linked the crate for the reasons the other person gave, but to be perfectly honest I wasn’t aware of that. Much appreciated!