Project 6: Implement an Iterator with Lifetimes

A custom iterator over a data structure that yields references with proper lifetime bounds.

Quick Reference

Attribute Value
Primary Language Rust
Alternative Languages None
Difficulty Level 3: Advanced
Time Estimate 1 week
Knowledge Area Iterator Pattern / Lifetimes
Tooling Rust std::iter
Prerequisites Projects 1-5

What You Will Build

A custom iterator over a data structure that yields references with proper lifetime bounds.

Why It Matters

This project builds core skills that appear repeatedly in real-world systems and tooling.

Core Challenges

  • Implementing the Iterator trait → maps to type Item and fn next(&mut self)
  • Lifetime bounds on the iterator → maps to ensuring references don’t outlive the source
  • Handling borrowing in next() → maps to returning Option<&'a T>
  • Iterator invalidation → maps to why you can’t mutate while iterating

Key Concepts

  • Iterator Trait: “The Rust Programming Language” Chapter 13
  • Lifetimes in Iterators: “Programming Rust” Chapter 15
  • Iterator Adapters: “Rust by Example” - Iterators chapter
  • Lending Iterators: Advanced topic (GATs - Generic Associated Types)

Real-World Outcome

Deliver a working demo with observable output that proves the feature is correct.


Implementation Guide

  1. Reproduce the simplest happy-path scenario.
  2. Build the smallest working version of the core feature.
  3. Add input validation and error handling.
  4. Add instrumentation/logging to confirm behavior.
  5. Refactor into clean modules with tests.

Milestones

  • Milestone 1: Minimal working program that runs end-to-end.
  • Milestone 2: Correct outputs for typical inputs.
  • Milestone 3: Robust handling of edge cases.
  • Milestone 4: Clean structure and documented usage.

Validation Checklist

  • Output matches the real-world outcome example
  • Handles invalid inputs safely
  • Provides clear errors and exit codes
  • Repeatable results across runs

References

  • Main guide: RUST_BORROW_CHECKER_LIFETIME_PHILOSOPHY.md
  • “Programming Rust” Chapter 15