Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING BASH RUST

Rust 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);
}
Try it Yourself »

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.");
}
Try it Yourself »

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);
Try it Yourself »

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);
Try it Yourself »

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);
}
Try it Yourself »

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.


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.