bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/Rust/Rust Data Structures
Rust•Rust Data Structures

Rust HashMap

Native lesson simulator

Hash map lookup

Hash the key, jump to a bucket, then compare entries there.

Ada -> bucket 20Cy:21Bo:22Ada:3Dee:33

Ada hashes to bucket 2; lookup only scans entries in that bucket.

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind Rust HashMap?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

___ std::collections::HashMap;
3Order

Put the learning moves in the order that makes the concept easiest to apply.

HashMaps are great when you want to store values and find them by a key.
A HashMap is a collection of key/value pairs.
Loop Through a HashMap

HashMap

A HashMap is a collection of key/value pairs.

HashMaps are great when you want to store values and find them by a key.

To use HashMap, you must import it from Rust's standard library:

use std::collections::HashMap;

Create a HashMap

You can create a new, empty HashMap and add items to it:

Example

// Import HashMap
use std::collections::HashMap;
fn main() {
  // Create a HashMap called capitalCities
  let mut capitalCities = HashMap::new();
  // Add keys and values (Country, City) capitalCities.insert("England", "London"); capitalCities.insert("Germany", "Berlin"); capitalCities.insert("Norway", "Oslo");
  println!("{:?}", capitalCities);
}

Access Values

You can use the .get() method to access a value in a HashMap by its key:

Example

let mut capitalCities = HashMap::new();
capitalCities.insert("England", "London");
capitalCities.insert("Germany", "Berlin");
capitalCities.insert("Norway", "Oslo");
if let Some(city) = capitalCities.get("England") {
  println!("The capital of England is {}.", city);
} else {
println!("England is not in the map.");
}

Update Values

If you insert a new value using a key that already exists, the old value is replaced with the new one:

Example

let mut
capitalCities = HashMap::new();
capitalCities.insert("England", "London");
capitalCities.insert("England", "Berlin");
println!("{:?}", capitalCities);

Remove Values

To remove a key from a HashMap, use the .remove() method:

Example

let mut capitalCities = HashMap::new();
// Add keys and values (Country, City) capitalCities.insert("England", "London"); capitalCities.insert("Germany", "Berlin"); capitalCities.insert("Norway", "Oslo"); // Remove the key "England" capitalCities.remove("England");
println!("{:?}", capitalCities);

Loop Through a HashMap

You can use a for loop to go through all key/value pairs:

Example

let mut capitalCities = HashMap::new();
// Add keys and values (Country, City) capitalCities.insert("England", "London"); capitalCities.insert("Germany", "Berlin"); capitalCities.insert("Norway", "Oslo"); // Loop through the HashMap
for (country, city) in
&capitalCities {
  println!("The capital of {} is {}.", country, city);
}

Why Use HashMaps?

  • To store data by key
  • To quickly look up values
  • To group related data (like names and scores)

Note

HashMaps require keys to be unique. Inserting the same key again will overwrite the old value.

Previous

Rust Tuples

Next

Rust Structs