summaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs19
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 {