From 2bfaddaa54e1d9c224e5b3376ac52ef93fd839d6 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Fri, 13 Jul 2018 13:58:43 +0300 Subject: Change: implement std::ops::AddAssign for Count --- src/main.rs | 19 ++++++++++++------- 1 file 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 { -- cgit v1.2.1