Closed mabushey closed 3 years ago
Is there a better way to read in a yaml file as a hashmap so I could use .iter()
, contains_key()
, get()
? The println() shows have a Hash, which I believe should be a Hashmap, but it seems gimped. Would using serde::yaml give me a hashmap with these traits?
This works great:
let mut junk = std::collections::HashMap::new();
junk.insert("junk.txt", "24b5235d525a954ab98798cdbf5b3663");
junk.insert("more_junk.exe", "d5a5d8421b2bd35ea6ab81f9d77efb3f");
println!("junk hashmap: {:?}", junk);
if junk.contains_key("junk.txt") {
println!("Match, hash is: {}", junk.get("junk.txt").unwrap());
}
junk hashmap: {"more_junk.exe": "d5a5d8421b2bd35ea6ab81f9d77efb3f", "junk.txt": "24b5235d525a954ab98798cdbf5b3663"}
Match, hash is: 24b5235d525a954ab98798cdbf5b3663
I'm not sure what's wrong with this crate but it seems like a much better solution is to just read the file in and use a regular expression to grab the values and insert into a hashmap. I'm not using any anchors or aliases in my yaml files so I think this this will be a much cleaner, robust, and working solution.
I was able use the serde-yaml crate (which uses this). I guess there's a reason for the serde middle-ware.
I'm pretty new to Rust. I've googled a few pages on this and either they're all wrong or it's changed. I'm using Rust 1.47.0 on Ubuntu. I have a config file in yaml.
println!("Config junk_file: {:?}", &doc["junk_file"]);
->I would like to do this (non-working code):
A lot of examples use
iter_mut()
or something like that. I want to read the values, not change them.