HashMaps
Rust is a systems programming language focused on speed, memory safety, and fearless concurrency — and the HashMap is its workhorse for mapping keys to values with fast lookups.
Learn HashMaps in our free Rust course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.
Part of the free Rust course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
In this lesson you'll create hash maps, insert and look up entries safely, and use the entry API to update values in place.
What You'll Learn in This Lesson
1️⃣ Creating, Inserting, and Looking Up
First bring it into scope with use std::collections::HashMap; . Then create one with HashMap::new() , add pairs with insert , and look up values with .get , which returns an Option so a missing key is handled safely.
Inserting the same key twice overwrote the old value — that's how you update an entry. Because .get returns an Option , looking up a missing key gives None rather than crashing.
2️⃣ The entry API and Iteration
The most elegant HashMap pattern is the entry API . map.entry(key).or_insert(default) inserts a default if the key is missing and hands you a mutable reference to the value either way — perfect for counting. You can also iterate over all pairs with for (k, v) in &map .
The line *counts.entry(word).or_insert(0) += 1 is the canonical frequency-count idiom. Remember a HashMap's order isn't guaranteed, so we summed a total rather than relying on iteration order.
Your turn. Fill in the blanks marked ___ , then run it.
Track quantities with a HashMap and use the entry API to update. Run it with cargo run .
📋 Quick Reference — HashMaps
Practice quiz
What does a HashMap<K, V> store?
- Only keys
- Only values
- Key-value pairs
- A sorted list
Answer: Key-value pairs. A HashMap maps keys to values for fast lookup by key.
Which import brings HashMap into scope?
- use std::collections::HashMap;
- use std::vec::HashMap;
- use std::map::HashMap;
- It is in the prelude
Answer: use std::collections::HashMap;. HashMap is not in the prelude; use std::collections::HashMap; imports it.
How do you create a new, empty HashMap?
- HashMap::default(0)
- HashMap::empty()
HashMap::new() creates an empty map.
What happens when you insert a key that already exists?
- It panics
- The old value is overwritten
- It is ignored
- It returns an error
Answer: The old value is overwritten. Inserting an existing key overwrites the old value.
What does .get(key) return?
- An Option
- The value directly
- A Result
- A reference that may dangle
Answer: An Option. .get returns Option<&V>: Some(&value) or None for a missing key.
What does entry(key).or_insert(0) return?
- A copy of the value
- true or false
- A mutable reference to the value
- The key
Answer: A mutable reference to the value. It inserts a default if missing and returns a mutable reference either way.
Which line is the canonical frequency-count idiom?
- map.insert(k, k+1)
- *map.entry(k).or_insert(0) += 1
- map.get(k).unwrap()
- map.count(k)
Answer: *map.entry(k).or_insert(0) += 1. *map.entry(k).or_insert(0) += 1 counts occurrences in place.
What is the iteration order of a HashMap?
- Sorted by key
- Insertion order
- Reverse order
- Not guaranteed
Answer: Not guaranteed. A HashMap has no guaranteed order; it can even vary between runs.
What happens if you index a HashMap with a missing key?
- Returns None
- Panics
- Returns 0
- Inserts the key
Answer: Panics. Indexing with map[key] panics on a missing key; prefer .get for safety.
Which collection keeps its keys sorted automatically?
- Vec
- HashMap
- BTreeMap
- HashSet
Answer: BTreeMap. BTreeMap keeps keys in sorted order, unlike HashMap.