Enter your book access code

The curriculum, drills, tutor, and interviewer mode are for readers of Ace the Rust Interview. The code is printed in the Preface (Companion site access).

Don’t have the book yet? The landing page stays open — purchase unlocks this gym.

intermediate · order 182

Custom Countdown iterator

iter-custom-01Related book chapters: 9 (optional — this page is complete on its own)iteratorcustomstate-machine

Slug: iter-custom-01 · Paired reading: Chapter 9

Prompt

Implement struct Countdown { remaining: u32 } with Countdown::new(n) and impl Iterator<Item = u32> that yields n, n-1, …, 1 then None. Yield nothing when n == 0.

Contract

  • After exhaustion, further next calls return None (fused-by-construction)
  • Store progress in the struct fields — do not hide the state machine in a range adapter inside next

Predict first

Where does the current value live between next calls?

Rust angle

Custom iterators are state machines: fields remember progress; next advances them.