9951 explained code solutions for 126 technologies


rustHow to iterate over a Rust HashMap?


Iterating over a Rust HashMap can be done using the iter() method. This method returns an iterator over the key-value pairs of the map.

Example code

let mut map = HashMap::new();
map.insert("a", 1);
map.insert("b", 2);

for (key, value) in map.iter() {
    println!("key: {} value: {}", key, value);
}

Output example

key: a value: 1
key: b value: 2

Code explanation

  • let mut map = HashMap::new();: creates a new empty HashMap
  • map.insert("a", 1);: inserts a key-value pair into the HashMap
  • map.iter(): returns an iterator over the key-value pairs of the map
  • for (key, value) in map.iter(): iterates over the key-value pairs of the map
  • println!("key: {} value: {}", key, value);: prints the key and value of the current iteration

Helpful links

Edit this code on GitHub