trait

Iterator — the next-item contract

Item identifies the yielded type. Each call to next advances or queries the iterator state. None means no item for that call; only a fused iterator guarantees permanent exhaustion after None. Expected stdout (not executed): Some(20) Some(21) Common mistake: Treating Item as always a reference.

Formal Syntax & Structure

rust syntaxrust
trait Iterator { type Item; fn next(&mut self) -> Option<Self::Item>; /* provided methods */ }

Working Example

Example Snippetrust
fn first_two<I: Iterator<Item = i32>>(mut source: I) {    println!("{:?} {:?}", source.next(), source.next());}fn main() { first_two(20..23); }
Rust Reference