trpl-zh-cn/listings/ch08-common-collections/listing-08-25/src/main.rs

17 lines
323 B
Rust
Raw Normal View History

2022-02-06 16:43:51 +08:00
fn main() {
// ANCHOR: here
use std::collections::HashMap;
2023-01-16 17:34:52 +08:00
let text = "hello world wonderful world";
2022-02-06 16:43:51 +08:00
2023-01-16 17:34:52 +08:00
let mut map = HashMap::new();
2022-02-06 16:43:51 +08:00
2023-01-16 17:34:52 +08:00
for word in text.split_whitespace() {
let count = map.entry(word).or_insert(0);
*count += 1;
}
println!("{:?}", map);
2022-02-06 16:43:51 +08:00
// ANCHOR_END: here
}