練習:計數器
在本練習中,您要使用非常簡單的資料結構並將其設為泛型。此結構會使用 std::collections::HashMap 追蹤出現過的值和出現次數。
Counter 的初始版本經過硬式編碼,僅適用於 u32 值。請設法讓結構體和相應的方法成為泛型,而非所追蹤值的型別,這樣 Counter 就可以追蹤任何型別的值。
如果您提前完成操作,不妨試著使用 entry 方法將實作 count 方法所需的雜湊查詢數量減半。
use std::collections::HashMap;
/// Counter counts the number of times each value of type T has been seen.
struct Counter {
values: HashMap<u32, u64>,
}
impl Counter {
/// Create a new Counter.
fn new() -> Self {
Counter {
values: 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;
} else {
self.values.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 main() {
let mut ctr = Counter::new();
ctr.count(13);
ctr.count(14);
ctr.count(16);
ctr.count(14);
ctr.count(14);
ctr.count(11);
for i in 10..20 {
println!("saw {} values equal to {}", ctr.times_seen(i), i);
}
let mut strctr = Counter::new();
strctr.count("apple");
strctr.count("orange");
strctr.count("apple");
println!("got {} apples", strctr.times_seen("apple"));
}