Given a list of values we would like to know how many times each value appears. The source could be any, but we had them in a vector here. The valus could be also anything, but in this case we have small strings.
examples/count-distribution-of-values/src/main.rs
use std::collections::HashMap;
fn main() {
let animals = vec![
"camel", "snake", "camel", "snake", "crab", "snake", "crab", "crab", "crab",
];
let counted = count_in_loop(&animals);
println!("{:#?}", counted);
}
fn count_in_loop<'a>(words: &'a [&'a str]) -> HashMap<&'a str, u32> {
let mut counter: HashMap<&str, u32> = HashMap::new();
for word in words {
*counter.entry(word).or_insert(0) += 1;
}
counter
}
#[test]
fn test_count() {
let words = vec![
"camel", "snake", "camel", "snake", "crab", "snake", "crab", "crab", "crab",
];
let expected = HashMap::from([("camel", 2), ("crab", 4), ("snake", 3)]);
assert_eq!(count_in_loop(&words), expected);
}