Info
This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: Extend HashMap with results from rayon parallel iterator
I'm trying to accumulate a HashMap with results from a parallel iterator of json file string data of football matches:
pub fn build(mut me: Self, raw_data: JsonFilesContentsAllRaw) -> anyhow::Result<Self> {
const ERROR_FUNC: &'static str = "IMDB::build";
for (year, files) in raw_data.iter() {
let hashmaps = files.par_iter().map(|(fname, contents)| {
let mut hashmap = HashMap::new();
match Self::turn_file_contents_to_hashmap(contents) {
Ok(map) => hashmap = map,
Err(err) => {
eprintln!(
"{ERROR_PREFIX} {ERROR_FUNC}: Error while parsing json file '{year}/{fname}': {err}"
);
}
}
hashmap
});
me.flat_hashmap.extend(hashmaps);
}
Ok(me)
}
fn turn_file_contents_to_hashmap(contents: &str) -> anyhow::Result<HashMap<String, Match>> {
const ERROR_FUNC: &'static str = "IMDB::turn_file_contents_to_hashmap";
let data: MatchList = serde_json::from_str(contents).with_context(|| {
format!("{ERROR_PREFIX} {ERROR_FUNC}: Could not parse json from file contents as a list of matches.")
})?;
Ok(HashMap::from_iter(data.matches.into_iter().map(|mch| {
let mut key = String::new();
key.push_str(&mch.team1);
key.push('_');
key.push_str(&mch.team2);
key.push('_');
key.push_str(&mch.date.to_string());
(key, mch)
})))
}
I'm getting this error:
65 | me.flat_hashmap.extend(hashmaps);
| ------ ^^^^^^^^ `rayon::iter::Map<rayon::collections::btree_map::Iter<'_, std::string::String, std::string::String>, {closure@src/imdb.rs:51:49: 51:68}>` is not an iterator
| |
| required by a bound introduced by this call
Do I need to collect
the results in a Vec
or something before extending the HashMap?
Thanks
2 posts - 1 participant
🏷️ Rust_feed