9951 explained code solutions for 126 technologies


rustHow to initialize a Rust HashMap?


A Rust HashMap is a collection of key-value pairs that use a hash table for storage. It is one of the most commonly used data structures in Rust.

To initialize a Rust HashMap, you can use the HashMap::new() method. This will create an empty HashMap with the default hasher.

use std::collections::HashMap;

let mut map = HashMap::new();

The code above creates an empty HashMap with the default hasher.

  1. use std::collections::HashMap;: This imports the HashMap type from the std::collections module.
  2. let mut map = HashMap::new();: This creates a mutable HashMap instance and assigns it to the map variable.

Helpful links

Edit this code on GitHub