Loading
0x90Lesson 10 of 17

Iterators and HashMap fundamentals

Transform collections idiomatically and count frequencies.

18 min 5-question quiz 1 code exercise
By the end of this lesson you can
  • Use iterator adapters and HashMap for practical data work.

Iterators provide composable collection processing through adapters like map, filter, and collect. HashMap<K, V> stores key-value associations efficiently.

main.rs
1use std::collections::HashMap;
2
3fn main() {
4    let words = ["rust", "safe", "rust"];
5    let mut counts: HashMap<&str, i32> = HashMap::new();
6
7    for word in words {
8        *counts.entry(word).or_insert(0) += 1;
9    }
10
11    println!("{}", counts["rust"]);
12}
Output
2

entry(...).or_insert(...) is the idiomatic way to update counters in a map. Iterator chains keep data transformations concise and expressive.

Try it

Up to 15 XP

Choose the right collection pattern

Sort each task by the iterator/hash-map pattern that solves it best.

0 of 4 sortedScore 0/0
  • “Create a vector of doubled numbers from a list”

  • “Count how many times each word appears”

  • “Keep only even numbers from a vector”

  • “Track request count per endpoint path”

Key takeaways

  • Use iterator adapters and HashMap for practical data work.

  • Rust uses types and ownership rules to prevent common bugs.

  • Small experiments build intuition quickly.

Lesson quiz

5 questions · pass with 4 correct · up to 50 XP

Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.

Practice: write Rust

These run for real. Write the program, press Run tests, and read what the compiler tells you - learning to read compiler messages is half of learning Rust.

Exercise 1

Count character frequencies

+25 XP

Read one line and print the count of lowercase a characters.

  • Multiple a letters
  • No a
main.rs
Loading editor…

Rust lessons are authored for modern stable Rust; exercises run on Rust 1.82 via Wandbox, a free public service. Your source code and test input are sent to that service.

Questions about this lesson

Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.

Loading posts…