9951 explained code solutions for 126 technologies


rustHow to create a Rust HashMap from a vector of tuples?


Creating a Rust HashMap from a vector of tuples is a simple process. The following example code creates a HashMap from a vector of tuples:

let mut map = HashMap::new();
let v = vec![(1, "a"), (2, "b"), (3, "c")];

for (key, value) in v {
    map.insert(key, value);
}

The output of the example code is:

HashMap { 1: "a", 2: "b", 3: "c" }

Code explanation

  1. let mut map = HashMap::new(); - This creates a new, empty HashMap.
  2. let v = vec![(1, "a"), (2, "b"), (3, "c")]; - This creates a vector of tuples.
  3. for (key, value) in v { - This starts a loop that iterates over each tuple in the vector.
  4. map.insert(key, value); - This inserts the key and value from the tuple into the HashMap.
  5. } - This ends the loop.

Helpful links

Edit this code on GitHub