summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2018-07-20 00:08:19 +0300
committerLars Wirzenius <liw@liw.fi>2018-07-20 00:08:19 +0300
commitc5fb6a609b0b315241bf53d6c3a822127c09da36 (patch)
tree30fb8b8c8fb60942f6e3b2d4a0e4681f3edb189e
parentcd7689adb5889ab957ced916280d2c9450d23a47 (diff)
downloadwordfreq-c5fb6a609b0b315241bf53d6c3a822127c09da36.tar.gz
Change: print only the 10 most common words
-rw-r--r--src/main.rs13
1 files changed, 11 insertions, 2 deletions
diff --git a/src/main.rs b/src/main.rs
index 4045cd7..2162453 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -12,7 +12,7 @@ fn main() -> io::Result<()> {
let mut f = File::open(&filename)?;
let mut reader = io::BufReader::new(f);
let counts = count_words(&mut reader)?;
- counts.print()
+ counts.print(10)
}
Ok(())
}
@@ -69,8 +69,17 @@ impl WordCounts {
*counter += count;
}
- fn print(&self) {
+ fn print(&self, max: usize) {
+ let mut top = Vec::new();
for (word, count) in self.counts.iter() {
+ top.push((count, word));
+ if top.len() > max {
+ top.sort();
+ top.reverse();
+ top.truncate(max);
+ }
+ }
+ for (count, word) in top.iter() {
println!("{} {}", count, word);
}
}