summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 76d4568dc4ddbf0564970beb393875a9746a1236 (plain)
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
use std::env;
use std::fs::File;
use std::io;
use std::io::BufRead;

mod counts;
use counts::*;

fn main() -> io::Result<()> {
    for filename in env::args().skip(1) {
        let mut f = File::open(&filename)?;
        let mut reader = io::BufReader::new(f);
        let counts = count_words(&mut reader)?;
        print(&counts, 10)
    }
    Ok(())
}


fn count_words(reader: &mut io::BufReader<File>) -> io::Result<WordCounts> {
    let mut counts = WordCounts::new();
    for line in reader.lines() {
        let line = line?;
        for w in line.split(|c: char| !c.is_alphabetic()).filter(|w| !w.is_empty()) {
            count(&mut counts, w.to_lowercase().clone());
        }
    }
    Ok(counts)
}