Koskamo

Iterators and closures

Document v1 · published by Koskamo editorial · updated 2026-10-04

Original source

Iterators handle the logic of walking a sequence of items, and in Rust they are lazy: creating one has no effect until you call a method that consumes it. All iterators implement the Iterator trait, whose only required method is next, returning one item at a time wrapped in Some and None when iteration is over; calling next changes internal state, so the iterator must be mutable and each call consumes an item. The iter method iterates over immutable references, into_iter takes ownership and returns owned values, and iter_mut iterates over mutable references. Methods that call next are consuming adaptors — sum, for example, takes ownership of the iterator, so it cannot be used afterward. Iterator adaptors such as map take a closure to apply to each item and produce a new iterator without consuming the original; because adaptors are lazy too, an unused map result only earns a warning until a consuming method like collect runs the closure and gathers the results into a collection. Scope: Rust. Reviewed October 4, 2026.

Source and provenance

Original source
doc.rust-lang.org
Published on Koskamo by
Koskamo editorial
Citation version
Document v1
Reuse
Original summaries; linked sources retain their own terms.

The original source supplies the underlying information. Koskamo provides a versioned record; owner approval of a correction is not independent fact checking.