Datasets:

Modalities:
Text
Formats:
text
Size:
< 1K
Libraries:
Datasets
License:
File size: 449 Bytes
998b6cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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"));
}