Tsallaka zuwa abun ciki
Justice Tech School

Practice: Word Frequency

Count words with a HashMap.

Example
fn main() {
    use std::collections::BTreeMap;
    let mut m = BTreeMap::new();
    for w in "the cat and the hat".split_whitespace() { *m.entry(w).or_insert(0) += 1; }
    println!("{:?}", m);
}

Result

{"and": 1, "cat": 1, "hat": 1, "the": 2}

Exercise

Split on spaces.

s.()

Quiz Test

Which map keeps keys sorted?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.