32 lines
653 B
Rust
32 lines
653 B
Rust
use std::collections::HashMap;
|
|
use std::io::{self, Write};
|
|
|
|
fn main() {
|
|
let mut input: String = String::new();
|
|
|
|
io::stdin()
|
|
.read_line(&mut input)
|
|
.unwrap();
|
|
|
|
let target: String = input.chars()
|
|
.filter(|c| c.is_alphanumeric() || c.is_whitespace())
|
|
.collect();
|
|
|
|
let answer = count_words(&target);
|
|
|
|
for (word, count) in &answer {
|
|
println!("{} {}", word, count);
|
|
}
|
|
}
|
|
|
|
fn count_words(text: &str) -> HashMap<&str, usize> {
|
|
let mut result: HashMap<&str, usize> = HashMap::new();
|
|
|
|
for word in text.split_whitespace() {
|
|
*result.entry(word).or_insert(0) += 1;
|
|
|
|
}
|
|
|
|
result
|
|
}
|