On this page
  1. Iterator
  2. Availability
  3. Syntax & Overview
  4. Notes & Pitfalls
  5. Example

Iterator

Prev: Drop | Up: builtins | Next: From
Rel: builtins, Ownership, Borrowing & Lifetimes
Ref: Rust Docs — Iterator
Tags: #public #prog #rust #builtins #tier/core


Availability

Built-in / Prelude (Prelude Trait) — available with zero use imports.

Syntax & Overview

trait Iterator { type Item; fn next(&mut self) -> Option<Self::Item>; }

Zero-cost lazy iteration protocol powering for loops and combinators (.map(), .filter(), .fold(), .collect()).

Notes & Pitfalls

Example

fn main() {
    let sum_of_even_squares: i32 = (1..=6)
        .filter(|n| n % 2 == 0)
        .map(|n| n * n)
        .sum();
    assert_eq!(sum_of_even_squares, 4 + 16 + 36);
}

Linked from

Related notes