I am trying to create a word count HashMap from an array of words using Entry and iterators in Rust. When I try as below,
use std::collections::HashMap;
fn main () {
let corpus = ["foo", "bar", "bar", "grok", "blah", "foo", "bar"];
let mut word_count = corpus.iter().fold(HashMap::new(), |mut acc, word| *acc.entry(word).or_insert(0) += 1);
println!("{:?}", word_count);
}
I get the error: expected struct HashMap, found (), type mismatch.
I can get it to work by creating a new scope and explicitly returning the accumulator as:
use std::collections::HashMap;
fn main () {
let corpus = ["foo", "bar", "bar", "grok", "blah", "foo", "bar"];
let mut word_count = corpus.iter().fold(HashMap::new(), |mut acc, word|{
*acc.entry(word).or_insert(0) += 1;
acc
});
println!("{:?}", word_count);
}
and I get the expected {"foo": 2, "bar": 3, "blah": 1, "grok": 1}.
Usually fold returns the last state of the accumulator, so I was expecting the first approach to work, and am unclear why it does not. Would really appreciate some clarification and any suggestions on alternative approaches (using iterators).