Skip to content

Commit 96b3582

Browse files
added hashmap part 1
1 parent 07ff804 commit 96b3582

1 file changed

Lines changed: 80 additions & 81 deletions

File tree

examples/07_hashmap_part_1.rs

Lines changed: 80 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,113 +1,112 @@
11
use std::collections::HashMap;
22

3-
fn main(){
4-
let mut scores: HashMap<String, i32> = HashMap::new();
5-
println!("empty map: {:?}, scores");
3+
fn main() {
4+
let mut scores: HashMap<String, i32> = HashMap::new();
5+
println!("empty map: {:?}, scores");
66

7-
// PART 2: Insert, Get, Remove
7+
// PART 2: Insert, Get, Remove
88

9-
let mut map: HashMap<String, String> = HashMap::new();
9+
let mut map: HashMap<String, String> = HashMap::new();
1010

11-
// insert - add a key-value pair
12-
map.insert(String::from("name"), String::from("alice"));
13-
map.insert(String::from("city"), String::from("Belarus"));
14-
map.insert(String::from("lang"), String::from("Rust"));
15-
println!("after inserts: {:?}", map);
11+
// insert - add a key-value pair
12+
map.insert(String::from("name"), String::from("alice"));
13+
map.insert(String::from("city"), String::from("Belarus"));
14+
map.insert(String::from("lang"), String::from("Rust"));
15+
println!("after inserts: {:?}", map);
1616

17-
// insert same key- OVERWRITES the old value
18-
map.insert(String::from("name"), String::from("Bheem"));
19-
println!("after overwrite: {:?}", map);
17+
// insert same key- OVERWRITES the old value
18+
map.insert(String::from("name"), String::from("Bheem"));
19+
println!("after overwrite: {:?}", map);
2020

21-
// get -- returns Option<&Value>
22-
match map.get("name") {
23-
Some(v) => println!("name = {}", v),
24-
None => println!("key not found"),
25-
}
21+
// get -- returns Option<&Value>
22+
match map.get("name") {
23+
Some(v) => println!("name = {}", v),
24+
None => println!("key not found"),
25+
}
2626

27-
// get a key that doesn't exist
28-
match map.get("age") {
29-
Some(v) => println!("age = {}", v),
30-
None => println!("age key does not exist"),
31-
}
27+
// get a key that doesn't exist
28+
match map.get("age") {
29+
Some(v) => println!("age = {}", v),
30+
None => println!("age key does not exist"),
31+
}
3232

33-
// contains_key - check if key exists
34-
println!("has city: {}", map.contains_key("city"));
35-
println!("has age: {}", map.contains_key("age"));
33+
// contains_key - check if key exists
34+
println!("has city: {}", map.contains_key("city"));
35+
println!("has age: {}", map.contains_key("age"));
3636

37-
// remove - delete a key, returns Option<Value>
38-
let removed = map.remove("city");
39-
println!("removed: {:?}", removed);
40-
println!("after remove: {:?}", map);
37+
// remove - delete a key, returns Option<Value>
38+
let removed = map.remove("city");
39+
println!("removed: {:?}", removed);
40+
println!("after remove: {:?}", map);
4141

42-
// PART 3: entry() - insert only if key doesn't exist
42+
// PART 3: entry() - insert only if key doesn't exist
4343

44-
let mut map: HashMap<String, i32> = HashMap::new();
45-
map.insert(String::from("alice"), 10);
44+
let mut map: HashMap<String, i32> = HashMap::new();
45+
map.insert(String::from("alice"), 10);
4646

47-
// or_insert - only inserts if key is NOT already there
48-
map.entry(String::from("alice")).or_insert(99); // alice exists -- no change
49-
map.entry(String::from("bob")).or_insert(50); // bob missing -- inserts 50
47+
// or_insert - only inserts if key is NOT already there
48+
map.entry(String::from("alice")).or_insert(99); // alice exists -- no change
49+
map.entry(String::from("bob")).or_insert(50); // bob missing -- inserts 50
5050

51-
println!("{:?}", map); // alice = 10, bob = 50
51+
println!("{:?}", map); // alice = 10, bob = 50
5252

53-
// or_insert returns a mutable reference -- you can modify it
54-
let alice_score = map.entry(String::from("alice")).or_insert(0);
55-
*alice_score += 5; // add 5 to alice's score
53+
// or_insert returns a mutable reference -- you can modify it
54+
let alice_score = map.entry(String::from("alice")).or_insert(0);
55+
*alice_score += 5; // add 5 to alice's score
5656

57-
println!("{:?}", map);
57+
println!("{:?}", map);
5858

59-
// PART 4: Looping over a hashmap
59+
// PART 4: Looping over a hashmap
6060

61-
let mut ages: HashMap<&str, u32> = HashMap::new();
62-
ages.insert("alice", 25);
63-
ages.insert("bob", 30);
64-
ages.insert("carol", 22);
61+
let mut ages: HashMap<&str, u32> = HashMap::new();
62+
ages.insert("alice", 25);
63+
ages.insert("bob", 30);
64+
ages.insert("carol", 22);
6565

66-
// loop over all key-value pairs
67-
// Note: Hashmap has NO guranteed order
66+
// loop over all key-value pairs
67+
// Note: Hashmap has NO guranteed order
6868

69-
for(name, age) in &ages{
70-
println!("{} is {}", name, age)
71-
}
69+
for (name, age) in &ages {
70+
println!("{} is {}", name, age)
71+
}
7272

73-
// Loop over just keys
74-
println!("keys");
75-
for key in ages.keys(){
76-
println!(" {}", key);
77-
}
73+
// Loop over just keys
74+
println!("keys");
75+
for key in ages.keys() {
76+
println!(" {}", key);
77+
}
7878

79-
// Loop over just values
80-
prontln!("values: ");
81-
for value in ages.values() {
82-
println!(" {}", value);
83-
}
79+
// Loop over just values
80+
prontln!("values: ");
81+
for value in ages.values() {
82+
println!(" {}", value);
83+
}
8484

85-
// PART 5 : Useful HashMap Methods
85+
// PART 5 : Useful HashMap Methods
8686

87-
let mut map: HashMap<&str, i32> = HashMap::new();
87+
let mut map: HashMap<&str, i32> = HashMap::new();
8888

89-
map.insert("a", 1);
90-
map.insert("b", 2);
91-
map.insert("c", 3);
89+
map.insert("a", 1);
90+
map.insert("b", 2);
91+
map.insert("c", 3);
9292

93-
println!("length: {}", map.len());
93+
println!("length: {}", map.len());
9494

95-
println!("is_empty ? : {}", map.is_empty());
95+
println!("is_empty ? : {}", map.is_empty());
9696

97-
let mut temp = map.clone();
98-
temp.clear();
99-
println!("after clear length: {}", temp.len());
97+
let mut temp = map.clone();
98+
temp.clear();
99+
println!("after clear length: {}", temp.len());
100100

101-
// PART 6: Word counter -- classic HashMap patter
101+
// PART 6: Word counter -- classic HashMap patter
102102

103-
let text = "hello world hello rust world hello";
104-
let mut word_count: HashMap<&str, i32> = HashMap::new();
105-
106-
for word in text.split_whitespace(){
107-
// get the count for this word, or start at 0
108-
let count = word_count.entry(word).or_insert(0);
109-
*count += 1;
110-
}
111-
println!("{:?}", word_count);
103+
let text = "hello world hello rust world hello";
104+
let mut word_count: HashMap<&str, i32> = HashMap::new();
112105

106+
for word in text.split_whitespace() {
107+
// get the count for this word, or start at 0
108+
let count = word_count.entry(word).or_insert(0);
109+
*count += 1;
110+
}
111+
println!("{:?}", word_count);
113112
}

0 commit comments

Comments
 (0)