Skip to main content

Writing your own Iterator

The `Iterator` trait has one required method: `next`, returning `Option<Self::Item>`. Return `Some(value)` while there is more, `None` when you are finished.

Everything else — `map`, `filter`, `take`, `sum`, `zip` — is a default method built on top of `next`. Implement the one, get the hundred.

This is the clearest demonstration of what traits are for: shared behaviour defined once, available to every type that opts in.

Run it and change it

The editor below is live: edit anything and the real compiler output updates by itself.

Your Rust