Core Beginner

05  Collections

Work with the two collections you'll reach for most: find a maximum safely, then tally word frequencies.

Step 2 / 2

Count occurrences of each word in word_count.

Hints
  • HashMap entry API: *map.entry(key).or_insert(0) += 1;
05_collections.rs
Reset to starterCompiled in a sandbox; hidden checks must pass.
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
}