summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 0bf2c9f4d3569224f7c4d870ec4344aa93d12894 (plain)
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
struct Container<K: Eq, V> {
    values: Vec<(K, V)>,
}

impl<K: Eq, V> Container<K, V> {
    fn new() -> Self {
        Self { values: vec![] }
    }

    fn insert(&mut self, k: K, v: V) {
        self.values.push((k, v));
    }

    fn get(&self, k: &K) -> Option<&V> {
        for (actual_k, v) in self.values.iter() {
            if actual_k == k {
                return Some(&v);
            }
        }
        None
    }
}

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!("{} -> {:?}", &alice, cont.get(&alice));
    println!("{} -> {:?}", &bob, cont.get(&bob));
    // Output should be Robert
}