9951 explained code solutions for 126 technologies


rustHow to create a Rust HashMap with a string key?


Creating a Rust HashMap with a string key is easy. The following example code creates a HashMap with a string key and a value of type i32:

use std::collections::HashMap;

let mut map = HashMap::new();
map.insert("key", 42);

The output of the code is:

()

The code consists of the following parts:

  1. use std::collections::HashMap; - imports the HashMap type from the standard library.
  2. let mut map = HashMap::new(); - creates a new empty HashMap.
  3. map.insert("key", 42); - inserts a key-value pair into the HashMap.

For more information, see the Rust documentation.

Edit this code on GitHub