1. ```
use std::collections::HashMap;
```
2. ```
use std::hash::Hash;
```
4. ```
/// Counter counts the number of times each value of type T has been seen.
```
5. ```
struct Counter<T> {
```
6. ```
values: HashMap<T, u64>,
```
7. ```
}
```
9. ```
impl<T: Eq + Hash> Counter<T> {
```
10. ```
/// Create a new Counter.
```
11. ```
fn new() -> Self {
```
12. ```
Counter { values: HashMap::new() }
```
13. ```
}
```
15. ```
/// Count an occurrence of the given value.
```
16. ```
fn count(&mut self, value: T) {
```
17. ```
*self.values.entry(value).or_default() += 1;
```
18. ```
}
```
20. ```
/// Return the number of times the given value has been seen.
```
21. ```
fn times_seen(&self, value: T) -> u64 {
```
22. ```
self.values.get(&value).copied().unwrap_or_default()
```
23. ```
}
```
24. ```
}
```
26. ```
fn main() {
```
27. ```
let mut ctr = Counter::new();
```
28. ```
ctr.count(13);
```
29. ```
ctr.count(14);
```
30. ```
ctr.count(16);
```
31. ```
ctr.count(14);
```
32. ```
ctr.count(14);
```
33. ```
ctr.count(11);
```
35. ```
for i in 10..20 {
```
36. ```
println!("saw {} values equal to {}", ctr.times_seen(i), i);
```
37. ```
}
```
39. ```
let mut strctr = Counter::new();
```
40. ```
strctr.count("apple");
```
41. ```
strctr.count("orange");
```
42. ```
strctr.count("apple");
```
43. ```
println!("got {} apples", strctr.times_seen("apple"));
```
44. ```
}
```