I'm using something similar to this answer to load some file data into a Rust binary. I'd like this data to be stored in a HashMap, so I'm able to search by key in the main program. However, I'm not sure how to do it in a way that ensures this data is immutable.
From the vector previously defined in build.rs
, I assume I could do something like this in main.rs
:
const ALL_THE_FILES: &[(&str, &[u8])] = &include!(concat!(env!("OUT_DIR"), "/all_the_files.rs"));
fn main () {
let all_the_files_hashmap: HashMap<&str, &[u8]> = ALL_THE_FILES.iter().cloned().collect();
// ...
}
Is there any way to construct this HashMap directly from build.rs
and load it to a const
in main.rs
? If I replace the tuple-vector approach used in the linked answer with a HashMap definition iterating with something like data.insert(<key>,<value>);
, can I load this into main.rs
with include!
in the same way, keeping the HashMap immutable?
Thanks a lot! Regards