summaryrefslogtreecommitdiff
path: root/rust/theBook/chapter-8-common-collections/hash/src/main.rs
blob: d1e9a4ca9c2a847eb5553a588fc3900aa5150561 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
use std::collections::HashMap as hm;

fn main() {
    let mut scores = hm::new();

    scores.insert(String::from("yellow"),10);
    scores.insert(String::from("blue"),50);

    let tm = String::from("blue");

    let score = scores.get(&tm).copied().unwrap_or(0);

    println!("{score}");

    count_word();
}


fn count_word() {
    let text = "hello world wonderful world";

    let mut hp = hm::new();

    for word in text.split_whitespace() {
        let count = hp.entry(word).or_insert(0);  // or_insert return reference
        *count += 1;
    }

    println!("{:#?}", hp);
}