I already posted a problem linked to this code, but it appears to be a different problem. I will try to describe the problem as clearly as possible.
Goal that I want to achieve
Create a hashmap with contents from a text file (in this case /proc/meminfo), similar to this format:
{
"MemTotal": "16256760 kB",
"MemFree": "462276 kB",
"MemAvailable": "10108672 kB",
"Buffers": "1432356 kB",
"Cached": "7138456 kB",
... etc
}
The raw output from the text file looks like this:
cat /proc/meminfo
MemTotal: 16256760 kB
MemFree: 2685512 kB
MemAvailable: 11105752 kB
Buffers: 774076 kB
Cached: 7722948 kB
... etc
The number of lines should be depending on how many lines a file contains.
Current situation
The code I currently have will create a hashmap with:
- key: "entryx".
- value: a line from the text file.
Code:
fn read_file_to_hashmap(path: impl AsRef<Path>) -> HashMap<String, String> {
let reader = BufReader::new(File::open(path).expect("error here"));
let hashmap = [
"Entry1".to_string(),
"Entry2".to_string(),
"Entry3".to_string(),
"Entry4".to_string(),
"Entry5".to_string(),
]
.into_iter()
.zip(reader.lines().map(Result::unwrap))
.collect::<HashMap<_, _>>();
return hashmap;
}
println! output:
{
"Entry1": "MemTotal: 16256760 kB",
"Entry5": "Cached: 7138456 kB",
"Entry2": "MemFree: 462276 kB",
"Entry4": "Buffers: 1432356 kB",
"Entry3": "MemAvailable: 10108672 kB",
}
How can I change the code to achieve the desired goal?