diff options
author | Lars Wirzenius <liw@liw.fi> | 2018-07-13 13:58:43 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2018-07-13 13:58:43 +0300 |
commit | 2bfaddaa54e1d9c224e5b3376ac52ef93fd839d6 (patch) | |
tree | 9e30d3f301a284ad01c81b767fb4aa6997e7054d /src/main.rs | |
parent | 7f9392e83b6795828e7cd3f82d80d24bf247fe25 (diff) | |
download | wc2-master.tar.gz |
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 19 |
1 files changed, 12 insertions, 7 deletions
diff --git a/src/main.rs b/src/main.rs index 4404d8b..37529c9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,9 +2,11 @@ use std::env; use std::fs::File; use std::io; use std::io::BufRead; +use std::ops; use std::result::Result; +#[derive(Debug, Clone, Copy)] struct Count { lines: usize, words: usize, @@ -22,12 +24,6 @@ impl Count { } } - fn add(&mut self, c: &Count) { - self.lines += c.lines; - self.words += c.words; - self.chars += c.chars; - } - fn count(&mut self, line: &str) { self.count_line(); self.count_words_in_line(line); @@ -61,13 +57,22 @@ impl Count { } +impl ops::AddAssign for Count { + fn add_assign(&mut self, other: Count) { + self.lines += other.lines; + self.words += other.words; + self.chars += other.chars; + } +} + + fn main() -> Result<(), io::Error> { let mut total = Count::new(); for filename in env::args().skip(1) { let mut f = File::open(&filename)?; let mut reader = io::BufReader::new(f); let c = wc(&mut reader)?; - total.add(&c); + total += c; println!("{:>5} {:>5} {:>5} {}", c.lines, c.words, c.chars, filename); } if env::args().skip(1).count() > 1 { |