std::iter¶
Status: experimental
Sequence adapters: map, filter, fold, zip, enumerate, chain, etc. A Vec argument is traversed eagerly; an Iterator argument keeps the adapter lazy and answers with another iterator.
Eager and lazy¶
These free functions traverse eagerly: a Vec argument is walked and the
result materialized. Laziness comes from the argument, not the spelling - an
Iterator<T> argument (xs.iter(), or a range) keeps map, filter,
take, skip, enumerate, chain, and zip lazy and answers with another
iterator, and a terminal consumes that state once. Use collect to
materialize it, or traverse a collection through its own methods
(xs.map(f), xs.sum()), which always answer eagerly.
See the lazy iterator protocol for ownership,
short-circuiting, overflow, and backend behavior.
API details and source¶
The implementation source contains the complete declarations and implementation notes. The table below lists canonical Gossamer call signatures; every item name links directly to its implementation file.
| Item | Canonical signature or declaration | Description |
|---|---|---|
all |
fn all<T>(items: Vec<T>, predicate: Fn(T) -> bool) -> bool |
True if every element satisfies f. |
any |
fn any<T>(items: Vec<T>, predicate: Fn(T) -> bool) -> bool |
True if any element satisfies f. |
chain |
fn chain<T>(left: Vec<T>, right: Vec<T>) -> Vec<T> |
Concatenates two sequences. |
chunk_by |
fn chunk_by<T, K: Eq>(items: Vec<T>, key: Fn(T) -> K) -> Map<K, Vec<T>> |
Groups elements into a map keyed by f. |
chunks |
fn chunks<T>(items: Vec<T>, n: i64) -> Vec<Vec<T>> |
Non-overlapping chunks of length n. |
collect |
fn collect<T>(items: Vec<T>) -> Vec<T> |
Materializes a sequence into a Vec. |
count |
fn count<T>(items: Vec<T>) -> i64 |
Number of elements. |
count_by |
fn count_by<T, K: Eq>(items: Vec<T>, key: Fn(T) -> K) -> Map<K, i64> |
Counts elements per key derived by f. |
dedup |
fn dedup<T: Eq>(items: Vec<T>) -> Vec<T> |
Removes consecutive duplicate elements. |
empty |
fn empty<T>() -> Vec<T> |
Empty Vec. |
enumerate |
fn enumerate<T>(items: Vec<T>) -> Vec<(i64, T)> |
Pairs each element with its index. |
filter |
fn filter<T>(items: Vec<T>, predicate: Fn(T) -> bool) -> Vec<T> |
Returns elements where f is true. |
filter_map |
fn filter_map<T, U>(items: Vec<T>, f: Fn(T) -> Option<U>) -> Vec<U> |
Maps each element and keeps the Some results. |
find |
fn find<T>(items: Vec<T>, predicate: Fn(T) -> bool) -> Option<T> |
First element satisfying f, or None. |
find_map |
fn find_map<T, U>(items: Vec<T>, f: Fn(T) -> Option<U>) -> Option<U> |
First Some result of f over the sequence. |
flat_map |
fn flat_map<T, U>(items: Vec<T>, f: Fn(T) -> Vec<U>) -> Vec<U> |
Maps f and flattens one level. |
flatten |
fn flatten<T>(items: Vec<Vec<T>>) -> Vec<T> |
Flattens a Vec |
fold |
fn fold<T, U>(items: Vec<T>, init: U, f: Fn(U, T) -> U) -> U |
Reduces a sequence with an accumulator. |
for_each |
fn for_each<T>(items: Vec<T>, f: Fn(T) -> ()) -> () |
Applies f to each element for its side effect. |
map |
fn map<T, U>(items: Vec<T>, f: Fn(T) -> U) -> Vec<U> |
Applies f to each element, returning a new Vec. |
max |
fn max<T: Ord>(items: Vec<T>) -> Option<T> |
Largest element, or None when empty. |
max_by |
fn max_by<T>(items: Vec<T>, compare: Fn(T, T) -> i64) -> Option<T> |
Largest element by the comparison closure. |
max_by_key |
fn max_by_key<T, K: Ord>(items: Vec<T>, key: Fn(T) -> K) -> Option<T> |
Element with the largest derived key. |
min |
fn min<T: Ord>(items: Vec<T>) -> Option<T> |
Smallest element, or None when empty. |
min_by |
fn min_by<T>(items: Vec<T>, compare: Fn(T, T) -> i64) -> Option<T> |
Smallest element by the comparison closure. |
min_by_key |
fn min_by_key<T, K: Ord>(items: Vec<T>, key: Fn(T) -> K) -> Option<T> |
Element with the smallest derived key. |
once |
fn once<T>(value: T) -> Vec<T> |
Single-element Vec containing value. |
pairwise |
fn pairwise<T>(items: Vec<T>) -> Vec<(T, T)> |
Consecutive overlapping pairs. |
partition |
fn partition<T>(items: Vec<T>, predicate: Fn(T) -> bool) -> (Vec<T>, Vec<T>) |
Splits into (matching, non-matching) by f. |
position |
fn position<T>(items: Vec<T>, predicate: Fn(T) -> bool) -> Option<i64> |
Index of the first element satisfying f, or None. |
product |
fn product(items: Vec<i64>) -> i64 |
Product of i64 or f64 elements. |
product_by |
fn product_by<T>(items: Vec<T>, f: Fn(T) -> i64) -> i64 |
Product of f(element) over the sequence. |
range |
fn range(start: i64, end: i64) -> Vec<i64> |
Half-open integer sequence [start, end). |
range_inclusive |
fn range_inclusive(start: i64, end: i64) -> Vec<i64> |
Closed integer sequence [start, end]. |
reduce |
fn reduce<T>(items: Vec<T>, f: Fn(T, T) -> T) -> Option<T> |
Folds with the first element as the initial accumulator. |
repeat |
fn repeat<T>(value: T, count: i64) -> Vec<T> |
A value repeated n times. |
rev |
fn rev<T>(items: Vec<T>) -> Vec<T> |
Returns a rev copy. |
scan |
fn scan<T, S, U>(items: Vec<T>, state: S, f: Fn(S, T) -> (S, Option<U>)) -> Vec<U> |
Folds while yielding each intermediate accumulator. |
skip |
fn skip<T>(items: Vec<T>, n: i64) -> Vec<T> |
All elements after the first n. |
skip_while |
fn skip_while<T>(items: Vec<T>, predicate: Fn(T) -> bool) -> Vec<T> |
Elements after the leading run satisfying f. |
sort_by |
fn sort_by<T>(items: Vec<T>, compare: Fn(T, T) -> i64) -> Vec<T> |
Sorted copy ordered by the comparison closure. |
sort_by_key |
fn sort_by_key<T, K: Ord>(items: Vec<T>, key: Fn(T) -> K) -> Vec<T> |
Sorted copy ordered by a derived key. |
step_by |
fn step_by<T>(items: Vec<T>, step: i64) -> Vec<T> |
Every step-th element, starting at index 0. |
sum |
fn sum(items: Vec<i64>) -> i64 |
Sum of i64 or f64 elements. |
sum_by |
fn sum_by<T>(items: Vec<T>, f: Fn(T) -> i64) -> i64 |
Sum of f(element) over the sequence. |
take |
fn take<T>(items: Vec<T>, n: i64) -> Vec<T> |
First n elements. |
take_while |
fn take_while<T>(items: Vec<T>, predicate: Fn(T) -> bool) -> Vec<T> |
Leading run of elements satisfying f. |
unzip |
fn unzip<A, B>(items: Vec<(A, B)>) -> (Vec<A>, Vec<B>) |
Splits a sequence of pairs into two Vecs. |
windows |
fn windows<T>(items: Vec<T>, n: i64) -> Vec<Vec<T>> |
Overlapping windows of width n. |
zip |
fn zip<A, B>(left: Vec<A>, right: Vec<B>) -> Vec<(A, B)> |
Pairs elements from two sequences. |