January 2019
Beginner to intermediate
554 pages
13h 31m
English
Rust also provides us with maps, which can be used to store key-value data. They come from the std::collections module and are named HashMap. They are created with the HashMap::new constructor function:
// hashmaps.rsuse std::collections::HashMap; fn main() { let mut fruits = HashMap::new(); fruits.insert("apple", 3); fruits.insert("mango", 6); fruits.insert("orange", 2); fruits.insert("avocado", 7); for (k, v) in &fruits { println!("I got {} {}", v, k); } fruits.remove("orange"); let old_avocado = fruits["avocado"]; fruits.insert("avocado", old_avocado + 5); println!("\nI now have {} avocados", fruits["avocado"]);}
In the preceding code, we created a new HashMap called fruits. We then insert some fruits into our fruits map, along ...
Read now
Unlock full access