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
37
38
39
40
41
42
43
44
45
46
47
48
49
use std::collections::HashMap;

pub struct Dict {
    map: HashMap<String, String>,
}

impl Dict {
    pub fn new() -> Self {
        Self {
            map: HashMap::new()
        }
    }

    /// dict.add(k,v): insert (k,v) into dict
    /// 
    /// ```
    /// e2c.add("john", "123");
    /// e2c.add("snoopy", "456");
    /// ```
    pub fn add(&mut self, k:&str, v:&str) {
        self.map.insert(k.to_string(), v.to_string());
    }

    /// dict.get(key): get value for key from dict
    /// ```
    /// e2c.get("snoopy");
    /// ```
    pub fn get(&mut self, k:&str)->Option<&String> {
        return self.map.get(k);
    }
}


#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn dict_test() {
        let mut e2c = Dict::new();
        e2c.add("a", "一隻");
        e2c.add("dog", "狗");
        e2c.add("cat", "貓");
        e2c.add("chase", "追");
        e2c.add("bite", "咬");
        assert!(e2c.get("cat") != None);
        assert!(e2c.get("xxx") == None);
    }
}