In the following function, I am creating a new HashMap with entries from a file (divided in name & value -> I am trying to read a meminfo file from Linux). I am stuck on how to return the values of the hashmap. Am I approaching this in the right way or should this be done differently?
fn read_file_to_hashmap(file_name: impl AsRef<Path>) -> HashMap<String, String> {
let file = BufReader::new(File::open(&file_name).expect("error here"));
let hash_map = ["name", "value"]
.into_iter()
.zip(file.lines().map(Result::unwrap))
.collect::<HashMap<_, _>>();
return hash_map;
}
Here is the error I get:
error[E0308]: mismatched types
--> src/main.rs:15:12
|
8 | fn read_file_to_hashmap(file_name: impl AsRef<Path>) -> HashMap<String, String> {
| ----------------------- expected `HashMap<std::string::String, std::string::String>` because of return type
...
15 | return hash_map;
| ^^^^^^^^ expected struct `std::string::String`, found `&str`
|
= note: expected struct `HashMap<std::string::String, _>`
found struct `HashMap<&str, _>`