Menu
Coddy logo textTech

Creating a Hash Map

Part of the Logic & Flow section of Coddy's Rust journey — lesson 28 of 66.

Now that you understand what hash maps are, let's learn how to create one. To use hash maps in Rust, you first need to bring the HashMap type into scope with a use statement at the top of your file:

use std::collections::HashMap;

Once you've imported HashMap, you can create a new, empty hash map using the HashMap::new() function:

let mut scores = HashMap::new();

Notice that we declare the hash map as mut because we'll likely want to add data to it later. When you create an empty hash map this way, Rust doesn't know what types the keys and values will be until you start inserting data.

You can also specify the types explicitly if you prefer:

let mut scores: HashMap<String, i32> = HashMap::new();

This creates a hash map that will store String keys (like student names) and i32 values (like test scores). The empty hash map is now ready to store your key-value pairs.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

Cheat sheet

To use hash maps in Rust, import HashMap from the standard library:

use std::collections::HashMap;

Create an empty hash map with implicit typing:

let mut scores = HashMap::new();

Create an empty hash map with explicit type annotation:

let mut scores: HashMap<String, i32> = HashMap::new();

Hash maps must be declared as mut if you plan to modify them. When using implicit typing, Rust infers the key and value types when you insert data.

Try it yourself

This lesson doesn't include a code challenge.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow