|
| 1 | +use std::collections::HashMap; |
| 2 | + |
| 3 | +fn main(){ |
| 4 | + let mut scores: HashMap<String, i32> = HashMap::new(); |
| 5 | + println!("empty map: {:?}, scores"); |
| 6 | + |
| 7 | + // PART 2: Insert, Get, Remove |
| 8 | + |
| 9 | + let mut map: HashMap<String, String> = HashMap::new(); |
| 10 | + |
| 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); |
| 16 | + |
| 17 | + // insert same key- OVERWRITES the old value |
| 18 | + map.insert(String::from("name"), String::from("Bheem")); |
| 19 | + println!("after overwrite: {:?}", map); |
| 20 | + |
| 21 | + // get -- returns Option<&Value> |
| 22 | + match map.get("name") { |
| 23 | + Some(v) => println!("name = {}", v), |
| 24 | + None => println!("key not found"), |
| 25 | + } |
| 26 | + |
| 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 | + } |
| 32 | + |
| 33 | + // contains_key - check if key exists |
| 34 | + println!("has city: {}", map.contains_key("city")); |
| 35 | + println!("has age: {}", map.contains_key("age")); |
| 36 | + |
| 37 | + // remove - delete a key, returns Option<Value> |
| 38 | + let removed = map.remove("city"); |
| 39 | + println!("removed: {:?}", removed); |
| 40 | + println!("after remove: {:?}", map); |
| 41 | + |
| 42 | + // PART 3: entry() - insert only if key doesn't exist |
| 43 | + |
| 44 | + let mut map: HashMap<String, i32> = HashMap::new(); |
| 45 | + map.insert(String::from("alice"), 10); |
| 46 | + |
| 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 |
| 50 | + |
| 51 | + println!("{:?}", map); // alice = 10, bob = 50 |
| 52 | + |
| 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 |
| 56 | + |
| 57 | + println!("{:?}", map); |
| 58 | + |
| 59 | + // PART 4: Looping over a hashmap |
| 60 | + |
| 61 | + let mut ages: HashMap<&str, u32> = HashMap::new(); |
| 62 | + ages.insert("alice", 25); |
| 63 | + ages.insert("bob", 30); |
| 64 | + ages.insert("carol", 22); |
| 65 | + |
| 66 | + // loop over all key-value pairs |
| 67 | + // Note: Hashmap has NO guranteed order |
| 68 | + |
| 69 | + for(name, age) in &ages{ |
| 70 | + println!("{} is {}", name, age) |
| 71 | + } |
| 72 | + |
| 73 | + // Loop over just keys |
| 74 | + println!("keys"); |
| 75 | + for key in ages.keys(){ |
| 76 | + println!(" {}", key); |
| 77 | + } |
| 78 | + |
| 79 | + // Loop over just values |
| 80 | + prontln!("values: "); |
| 81 | + for value in ages.values() { |
| 82 | + println!(" {}", value); |
| 83 | + } |
| 84 | + |
| 85 | + // PART 5 : Useful HashMap Methods |
| 86 | + |
| 87 | + let mut map: HashMap<&str, i32> = HashMap::new(); |
| 88 | + |
| 89 | + map.insert("a", 1); |
| 90 | + map.insert("b", 2); |
| 91 | + map.insert("c", 3); |
| 92 | + |
| 93 | + println!("length: {}", map.len()); |
| 94 | + |
| 95 | + println!("is_empty ? : {}", map.is_empty()); |
| 96 | + |
| 97 | + let mut temp = map.clone(); |
| 98 | + temp.clear(); |
| 99 | + println!("after clear length: {}", temp.len()); |
| 100 | + |
| 101 | + // PART 6: Word counter -- classic HashMap patter |
| 102 | + |
| 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); |
| 112 | + |
| 113 | +} |
0 commit comments