diff --git a/src/std-types/exercise.md b/src/std-types/exercise.md index 7c1979f0d001..e9f236f2f59f 100644 --- a/src/std-types/exercise.md +++ b/src/std-types/exercise.md @@ -15,7 +15,7 @@ It uses a to keep track of what values have been seen and how many times each one has appeared. -The initial version of `Counter` is hardcoded to only work for `u32` values. +The initial version of `Counter` is hardcoded to only work for `char` values. Make the struct and its methods generic over the type of value being tracked, that way `Counter` can track any type of value. @@ -32,29 +32,29 @@ use std::collections::HashMap; /// Counter counts the number of times each value of type T has been seen. struct Counter { - values: HashMap, + counts: HashMap, } impl Counter { /// Create a new Counter. fn new() -> Self { Counter { - values: HashMap::new(), + counts: HashMap::new(), } } /// Count an occurrence of the given value. - fn count(&mut self, value: u32) { - if self.values.contains_key(&value) { - *self.values.get_mut(&value).unwrap() += 1; + fn count(&mut self, value: char) { + if self.counts.contains_key(&value) { + *self.counts.get_mut(&value).unwrap() += 1; } else { - self.values.insert(value, 1); + self.counts.insert(value, 1); } } /// Return the number of times the given value has been seen. - fn times_seen(&self, value: u32) -> u64 { - self.values.get(&value).copied().unwrap_or_default() + fn times_seen(&self, value: char) -> u64 { + self.counts.get(&value).copied().unwrap_or_default() } } diff --git a/src/std-types/exercise.rs b/src/std-types/exercise.rs index a98565601907..384eb5fefc0f 100644 --- a/src/std-types/exercise.rs +++ b/src/std-types/exercise.rs @@ -20,38 +20,48 @@ use std::hash::Hash; /// Counter counts the number of times each value of type T has been seen. struct Counter { - values: HashMap, + counts: HashMap, } impl Counter { /// Create a new Counter. fn new() -> Self { - Counter { values: HashMap::new() } + Counter { counts: HashMap::new() } } /// Count an occurrence of the given value. - fn count(&mut self, value: T) { - *self.values.entry(value).or_default() += 1; + fn count(&mut self, item: T) { + *self.counts.entry(item).or_default() += 1; } /// Return the number of times the given value has been seen. - fn times_seen(&self, value: T) -> u64 { - self.values.get(&value).copied().unwrap_or_default() + fn times_seen(&self, item: T) -> u64 { + self.counts.get(&item).copied().unwrap_or_default() } } // ANCHOR: main fn main() { - let mut ctr = Counter::new(); - ctr.count(13); - ctr.count(14); - ctr.count(16); - ctr.count(14); - ctr.count(14); - ctr.count(11); + let mut charctr = Counter::new(); + charctr.count(' '); + charctr.count('a'); + charctr.count('¥'); + charctr.count('a'); + println!( + "most common character: {:?}", + charctr.counts.keys().max_by_key(|&c| charctr.times_seen(*c)) + ); + + let mut intctr = Counter::new(); + intctr.count(13); + intctr.count(14); + intctr.count(16); + intctr.count(14); + intctr.count(14); + intctr.count(11); for i in 10..20 { - println!("saw {} values equal to {}", ctr.times_seen(i), i); + println!("saw {} values equal to {}", intctr.times_seen(i), i); } let mut strctr = Counter::new();