Rust HashMap Explained: insert, get, entry and Counting Words
November 10, 2026
Welcome to episode 16 of Rust for Beginners, a series for complete beginners (no programming experience needed). In the last video, we learned about vectors. Today, we'll learn HashMap: a way to store values by a key, like words in a dictionary. Every code block below is the exact code from the video, ready to copy.
- All episodes are in the Rust Tutorial for Beginners playlist.
- Versions used: Rust 1.99.1 on Ubuntu, VS Code with rust-analyzer.
1. New project
Let's create a new project called scores, and open it in VS Code.
cargo new scores
cd scores2. use
HashMap isn't loaded by default, so we bring it in with use, from the standard library's collections.
src/main.rs:
use std::collections::HashMap;
fn main() {
let mut scores = HashMap::new();
scores.insert("Blue", 10);
scores.insert("Red", 50);
println!("Blue: {:?}", scores.get("Blue"));
println!("Green: {:?}", scores.get("Green"));
let red = scores.get("Red").copied().unwrap_or(0);
println!("Red has {red} points");
}Output: Blue: Some(10) / Green: None / Red has 50 points
3. insert
We create an empty map, and insert pairs of a key and a value. Here, a team name and its score.
4. get
get looks up a key. The key might not exist, so it returns an Option.
5. Default
To get a plain number with a default of zero, we chain copied, and unwrap or.
6. Run A
Let's run it. Blue is Some ten, Green is None, and Red has fifty points.
cargo run7. Overwrite
Inserting the same key again replaces the old value. Blue is now twenty-five.
use std::collections::HashMap;
fn main() {
let mut scores = HashMap::new();
scores.insert("Blue", 10);
scores.insert("Red", 50);
scores.insert("Blue", 25);
for (team, score) in &scores {
println!("{team}: {score}");
}
scores.remove("Red");
println!("Has Red? {}", scores.contains_key("Red"));
}Output: Red: 50 and Blue: 25 (in either order) / Has Red? false
8. Loop
We can loop over all pairs. Each round gives us a key and a value.
9. Order
Let's run it a few times. Notice the order can change. A HashMap doesn't keep any order.
cargo run10. remove
remove deletes a key, and contains key checks if a key is there.
11. Count words
A classic use: counting words. We split the text into words, and count each one.
use std::collections::HashMap;
fn main() {
let text = "the cat and the dog and the bird";
let mut counts = HashMap::new();
for word in text.split_whitespace() {
let count = counts.entry(word).or_insert(0);
*count += 1;
}
println!("the: {}", counts["the"]);
println!("and: {}", counts["and"]);
println!("cat: {}", counts["cat"]);
}Output: the: 3 / and: 2 / cat: 1
12. entry
entry, or insert, gets the count for this word, or inserts zero if it's new. Then we add one through the reference, with a star.
13. Run C
Let's run it. "the" appears three times, "and" twice, and "cat" once.
cargo runRecap
To recap: a HashMap stores values by key. insert adds or replaces, get returns an Option, entry or insert updates a value, and there's no fixed order.