1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
use std::collections::HashMap;
use std::hash::Hash;
#[derive(Debug)]
struct Container<K: Hash + Eq + PartialEq, V> {
values: HashMap<K, V>,
}
impl<K: Hash + Eq + PartialEq, V> Container<K, V> {
fn new() -> Self {
Self {
values: HashMap::new(),
}
}
fn insert(&mut self, k: K, v: V) {
self.values.insert(k, v);
}
fn get(&self, k: &K) -> Option<&V> {
self.values.get(k)
}
}
fn main() {
let alice = "alice".to_string();
let bob = "bob".to_string();
let robert = "Robert".to_string();
let mut cont = Container::new();
cont.insert(bob.clone(), bob.clone());
cont.insert(bob.clone(), robert);
println!("values: {:?}", cont);
println!("{} -> {:?}", &alice, cont.get(&alice));
println!("{} -> {:?}", &bob, cont.get(&bob));
// Output should be Robert
}
|