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
50
51
52
53
54
// Hash Maps
use std::collections::HashMap;
fn main() {
let mut person: HashMap<&str, i32> = HashMap::new();
// here insert data to hash map
person.insert("Nouman", 40);
person.insert("Kamran", 39);
person.insert("Shahid", 55);
// the get function is used to retrieve some specific entry
println!(
"The age of Nouman is {}",
person.get("Nouman").unwrap()
);
if person.contains_key("Nouman") {
println!("The value exist");
} else {
println!("The value does not exist");
}
// use pattern match to repalce if/else logic
match person.get("Nouman") {
// here the Some(...) should pass a variable if we need to use it in the pattern match
// expression
Some(value) => {
println!("The value of Norman is {} it is exist", value)
}
None => println!("The value of Nouman does not exist!"),
}
// here is another way to traverse entries in hash map
for (name, age) in person {
println!("The person {} has an age of {}", name, age);
}
// another case for hash map
let mut likes: HashMap<&str, &str> = HashMap::new();
likes.insert("mae", "video games");
// the second operation will overwrite the value
likes.insert("mae", "meat");
println!("likes hash table is {:?}", likes);
// here we use this expression to do the following things
// first check whether the key's has a correspoinding value exists in the HashMap
// if not, then insert the correspoinding value to the map
likes.entry("key").or_insert("steam");
likes.entry("key").or_insert("ps5");
// then print the value, then, this time the second insert operation will not be overwritten an already exist value
println!("likes hash map {:?}", likes);
}