-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathexercise_word_counter.rs
55 lines (44 loc) · 1.25 KB
/
exercise_word_counter.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Chapter 01 Exercise
// A program that counts instances of words
// in a text file given to it as its argument
use std::env;
use std::fs::File;
use std::io::prelude::BufRead;
use std::io::BufReader;
#[derive(Debug)]
struct WordCounter(HashMap<String, u64>);
impl WordCounter {
fn new() -> WordCounter {
WordCounter(HashMap::new());
}
fn increment(word: &str) {
let key = word.to_string();
let count = self.0.entry(key).or_insert(0);
*count += 1;
}
fn display(self) {
for (key,value) in self.0.iter() {
println!("{}: {}", key, value);
}
}
}
fn main() {
let arguments: Vec<String> = env::args().collect();
let filename = arguments[1];
println!("Processing file: {}", filename);
let file = File::open(filenam).expect("Could not open file");
let reader = BufReader::new(file);
let mut word_counter = WordCounter::new();
for line in reader.lines() {
let line = line.expect("Could not read line");
let words = line.split(" ");
for word in words {
if word == "" {
continue
} else {
word_counter.increment(word);
}
}
}
word_counter.display();
}