From d8a09d7c974638628a36c77c6eafa6441da16ca7 Mon Sep 17 00:00:00 2001 From: Frances Wingerter Date: Fri, 11 Sep 2026 15:22:18 -0400 Subject: [PATCH] exercises: avoid confusing names in std types exercise the exercise used to call the map from values to their counts `values, but it maps *from* values to their counts. it is more aptly called `counts`. additionally, mapping from u32 to u64 in the initial monomorphic implementation make it less than obvious which integer type is the type of the values being counted and which integer type is the count itself. switch the type initially being counted to char to remove this ambiguity. finally, do not use `value` as the variable name for the individual values of the type being counted. though this is technically accurate, it is an unfortunate collision with the fact that hash maps are a key-value data structure, and in this case the keys of the hash map are the values being counted, while its values are the per-value counts. instead, we count "item"s. --- src/std-types/exercise.md | 18 +++++++++--------- src/std-types/exercise.rs | 38 ++++++++++++++++++++++++-------------- 2 files changed, 33 insertions(+), 23 deletions(-) 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();