Take the following code snippet:
use std::collections::HashMap;
fn main() {
let mut en_fr_map: HashMap<String, String> = HashMap::new();
en_fr_map.insert(String::from("Hello"), String::from("Bonjour"));
en_fr_map.insert(String::from("World"), String::from("Monde"));
for (key, value) in en_fr_map {
println!("key is {} and value is {}", key, value);
}
}
Which compiles fines and spits out the following output:
key is Hello and value is Bonjour
key is World and value is Monde
Now, if I change the for loop to this:
for (key, value) in en_fr_map.iter() {
println!("key is {} and value is {}", key, value);
}
That is, calling the iter()
function on the map, the result is the same. How are they different?