Iterators

Rust is a systems programming language focused on speed, memory safety, and fearless concurrency — and iterators let you process sequences lazily and expressively, with zero runtime cost.

Learn Iterators 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 chain adapters like map and filter , finish with consumers like collect and sum , and understand laziness.

What You'll Learn in This Lesson

1️⃣ Adapters: map, filter, and collect

Call .iter() on a collection to get an iterator, then chain adapters that transform it: filter keeps matching items, map transforms each one. Adapters are lazy — they don't run until a consumer like collect or sum drives the chain.

The whole filter().map() pipeline did nothing until collect() ran it. That laziness is what makes long chains efficient — no throwaway intermediate vectors.

2️⃣ Consumers: count, find, enumerate, and Ranges

Beyond collect , many consumers produce a single value: count , max , find (returns an Option ). The enumerate adapter pairs each item with its index. And ranges like 1..=4 are iterators themselves.

Notice find returned an Option — there might be no match. And summing the squares of a range showed how adapters and consumers compose over anything iterable.

Your turn. Fill in the blanks marked ___ , then run it.

Use adapters and consumers to count, find the max, and filter a list of scores. Run it with cargo run .

📋 Quick Reference — Iterators

Practice quiz

What does it mean that iterator adapters are lazy?

  • They run immediately
  • They only work on numbers
  • They do no work until a consumer drives the chain
  • They cache results

Answer: They do no work until a consumer drives the chain. Adapters build a description; nothing runs until a consumer pulls values.

Which of these is an iterator adapter?

  • map
  • collect
  • sum
  • count

Answer: map. map returns a new iterator, so it is an adapter, not a consumer.

Which of these is a consumer?

  • filter
  • map
  • enumerate
  • collect

Answer: collect. collect runs the chain to completion and builds a final collection.

What does .filter() do?

  • Transforms each item
  • Keeps only items matching a condition
  • Sums items
  • Reverses the order

Answer: Keeps only items matching a condition. filter keeps only the elements for which the closure returns true.

What does .map() do?

  • Transforms each item
  • Keeps some items
  • Counts items
  • Sorts items

Answer: Transforms each item. map applies a closure to transform every element.

What does .collect() do?

  • Adds numbers
  • Finds the max
  • Builds a collection from the iterator
  • Prints items

Answer: Builds a collection from the iterator. collect consumes the iterator into a collection like a Vec.

What does .find() return?

  • A Vec
  • An Option
  • A bool
  • The index

Answer: An Option. find returns the first matching item as an Option (Some or None).

What does .enumerate() produce?

  • Only values
  • Only indexes
  • A single count
  • Pairs of (index, item)

Answer: Pairs of (index, item). enumerate pairs each item with its position index.

How do you typically end an iterator chain?

  • With many consumers
  • With exactly one consumer
  • With no consumer
  • With a semicolon only

Answer: With exactly one consumer. A chain is several adapters followed by exactly one consumer.

How fast are idiomatic iterator chains compared to hand-written loops?

  • Much slower
  • Slightly slower always
  • Just as fast (zero-cost)
  • Only fast in release with unsafe

Answer: Just as fast (zero-cost). Iterators are a zero-cost abstraction, compiling to loop-equivalent code.