get frequence in rust

main.rs
use std::collections::HashMap;

fn main() {
    let mut word_freq = HashMap::new();
    let words = vec!["apple", "banana", "apple", "cherry", "banana", "apple"];

    for word in words {
        let count = word_freq.entry(word).or_insert(0);
        *count += 1;
    }

    for (word, freq) in word_freq {
        println!("{}: {}", word, freq);
    }
}
356 chars
16 lines

related categories

gistlibby LogSnag