use std::collections::HashMap; | |
use std::fs; | |
fn word_frequency(file_path: &str) -> HashMap<String, u32> { | |
let contents = fs::read_to_string(file_path).expect("Something went wrong reading the file"); | |
let mut map = HashMap::new(); | |
for word in contents.split_whitespace() { | |
let count = map.entry(word.to_string()).or_insert(0); | |
*count += 1; | |
} | |
map | |
} | |
fn main() { | |
println!("{:?}", word_frequency("sample.txt")); | |
} |