std::iterIterator combinators for Vec<T> and any Iterator implementation.
This module exposes the lazy Iterator-based combinator API (Map,
Filter, Take, Skip, and the terminal helpers fold, count,
collect). Each wrapper is a pub type record that itself implements
the Iterator trait declared in std/builtins.hew, so adapters compose
through chained constructor calls:
import std.iter;
fn main() {
var v: Vec<i64> = Vec.new();
v.push(1);
v.push(2);
v.push(3);
let it = v.iter();
let doubled_first_two = iter.take(iter.map(it, |x: i64| x * 2), 2);
println(iter.collect(doubled_first_two).len()); // 2
}
Iterator.next takes a mutable receiver (fn next(var self) -> Option<Self.Item>).
A manually driven iterator needs a var binding; a for loop advances it
automatically. Adapters and terminal helpers consume their input iterator.
After passing an iterator to an adapter, use the returned adapter to continue.
| Wrapper | Constructor | Item projection |
|---|---|---|
Map<I,A,B> | map(it, f) | B from fn(A) -> B |
Filter<I,A> | filter(it, pred) | A (subset of inner) |
Take<I,A> | take(it, n) | inner's Item (A) |
Skip<I,A> | skip(it, n) | inner's Item (A) |
Terminal helpers (fold, count, collect, any, all, sum,
sum_f64, product, product_f64) consume any Iterator. Callers
drive a Vec<T> through the lazy surface via Vec.into_iter(), e.g.
iter.collect(iter.map(v.into_iter(), f)).
mapConstruct a lazy Map adapter over it applying f to each item.
filterConstruct a lazy Filter adapter over it keeping items for which
pred returns true.
takeConstruct a lazy Take adapter that yields at most n items from
it.
skipConstruct a lazy Skip adapter that discards the first n items
from it.
enumerateConstruct a lazy Enumerate adapter over it.
foldLeft-fold over it: walk every item applying f(acc, item) and
return the accumulated value.
countReturn the number of items produced by it.
collectCollect every item produced by it into a Vec<A>.
anyReturn true if f returns true for at least one item produced by
it. Short-circuits on the first match.
allReturn true if f returns true for every item produced by it.
Short-circuits on the first non-match.
findReturn the first item for which pred returns true, or None.
Like any, this short-circuits as soon as the predicate matches; items
observed before the match are released by their lexical match arms.
sumReturn the sum of every i64 item produced by it.
sum_f64Return the sum of every f64 item produced by it.
productReturn the product of every i64 item produced by it. Returns 1
for an empty iterator (the multiplicative identity).
product_f64Return the product of every f64 item produced by it. Returns 1.0
for an empty iterator (the multiplicative identity).
MapLazy map adapter: yields f(x) for each item produced by the inner
iterator.
A is the inner Item type (carried in the type parameters so the
closure's input projection is explicit); B is the produced Item.
FilterLazy filter adapter: yields only those items for which pred returns
true. Inner Item and produced Item agree on A.
TakeLazy take adapter: yields at most remaining items from the inner
iterator, then reports None.
A is the inner Item type. It is carried as a direct type parameter
(rather than left projection-only in the impl bound) so a concrete site
— Take<Countdown, i64> — pins the item type in the record arguments, which
is what the cross-module monomorphisation collector keys on. Map/Filter
carry their item type the same way; without it a projection-only A
stays free and terminals (count/collect/fold) fail to monomorphise
(mir-gap-where-clause-proj-monomorph).
SkipLazy skip adapter: discards the first remaining items, then yields
the rest of the inner iterator unchanged.
A is the inner Item type, carried as a direct type parameter for
the same monomorphisation reason as Take (see above): a projection-
only item param cannot be pinned by the collector, so terminals over
Skip fail to monomorphise.
EnumerateLazy enumerate adapter: pairs each item with its zero-based position.
The index advances only when the inner iterator yields an item, so an exhausted iterator remains exhausted without manufacturing a final index.