rust-syntax-iterators-closures
rust-syntax-iterators-closures
The mechanics of Rust's iterator and closure systems, treated as one topic because closures are predominantly authored as arguments to iterator adapters and they share the same lifetime and trait-bound mental model. Covers the Iterator trait contract, the standard adapter set, lazy evaluation, custom Iterator impls, IntoIterator vs Iterator, the Fn / FnMut / FnOnce trait hierarchy, move closures, edition-2021 disjoint captures, async closures (Rust 1.85), and returning closures.
Cross-references: [[rust-syntax-traits]] (Iterator IS a trait, closure traits are traits) [[rust-syntax-async-await]] (async closures return impl Future) [[rust-syntax-ownership]] (move captures, by-value vs by-reference) [[rust-syntax-generics]] (generic bounds T: Iterator, F: Fn(...)) [[rust-syntax-trait-objects]] (Box<dyn Fn> vs impl Fn).
When to use this skill
- User writes any iterator chain:
.map(...).filter(...).collect() - User asks "why doesn't my loop / iterator do anything?" (lazy evaluation)
- User implements a custom
Iteratorfor their own type - User confuses
.iter(),.iter_mut(), and.into_iter() - User picks between
Fn,FnMut,FnOncefor a closure-taking API - User hits "closure may outlive borrowed value" and needs
move - User writes
async || { ... }(Rust 1.85+) and needs the trait story - User wants to return a closure from a function
- User asks about
collect::<Vec<_>>()turbofish syntax