Core Beginner
05 Collections
Work with the two collections you'll reach for most: find a maximum safely, then tally word frequencies.
Count occurrences of each word in word_count.
Hints
- HashMap entry API:
*map.entry(key).or_insert(0) += 1;
Reveal solution
use std::collections::HashMap;
fn word_count(words: &[&str]) -> HashMap<String, u32> {
let mut counts = HashMap::new();
for w in words {
*counts.entry(w.to_string()).or_insert(0) += 1;
}
counts
}