Module std::iter

Iterator 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:

let doubled_first_two = iter::take(iter::map(it, |x: i64| x * 2), 2);

Iterator::next takes a mutable receiver (fn next(var self) -> Option<Self::Item>). Call sites write the returned Self state back into the caller binding; the for-loop desugar that will introduce and drive that binding lands in a later stage of the iteration-foundation lane.

Lazy combinator surface

WrapperConstructorItem 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>take(it, n)inner's Item
Skip<I>skip(it, n)inner's Item

Terminal helpers (fold, count, collect) consume any Iterator.

The legacy eager helpers below (map_int, filter_str, ...) are kept transiently while the Vec<T>: IntoIterator impl is being wired in a follow-on stage; once Vec::into_iter lands, callers migrate to iter::map(v.into_iter(), ...) and the eager table is retired in a single subtractive commit.

Contents

Functions

Function map

pub fn map(it: I, f: fn(A) -> B) -> Map<I, A, B>

Construct a lazy Map adapter over it applying f to each item.

Function filter

pub fn filter(it: I, pred: fn(A) -> bool) -> Filter<I, A>

Construct a lazy Filter adapter over it keeping items for which pred returns true.

Function take

pub fn take(it: I, n: i64) -> Take<I>

Construct a lazy Take adapter that yields at most n items from it.

Function skip

pub fn skip(it: I, n: i64) -> Skip<I>

Construct a lazy Skip adapter that discards the first n items from it.

Function fold

pub fn fold(it: I, init: B, f: fn(B, A) -> B) -> B

Left-fold over it: walk every item applying f(acc, item) and return the accumulated value.

Function count

pub fn count(it: I) -> i64

Return the number of items produced by it.

Function collect

pub fn collect(it: I) -> Vec<A>

Collect every item produced by it into a Vec<A>.

Function map_int

pub fn map_int(v: Vec<i64>, f: fn(i64) -> i64) -> Vec<i64>

Apply f to every element and return a new vector of results.

Examples

let v: Vec<i64> = Vec::new();
v.push(1);
v.push(2);
let doubled = iter.map_int(v, |x: i64| x * 2);
// doubled == [2, 4]

Function filter_int

pub fn filter_int(v: Vec<i64>, f: fn(i64) -> bool) -> Vec<i64>

Return a new vector containing only elements for which f returns true.

Examples

let v: Vec<i64> = Vec::new();
v.push(1);
v.push(2);
v.push(3);
let evens = iter.filter_int(v, |x: i64| x % 2 == 0);
// evens == [2]

Function fold_int

pub fn fold_int(v: Vec<i64>, init: i64, f: fn(i64, i64) -> i64) -> i64

Left-fold: accumulate all elements starting from init.

Examples

let v: Vec<i64> = Vec::new();
v.push(1);
v.push(2);
v.push(3);
let total = iter.fold_int(v, 0, |acc: i64, x: i64| acc + x);
// total == 6

Function any

pub fn any(v: Vec<i64>, f: fn(i64) -> bool) -> bool

Return true if f returns true for at least one element.

Examples

let v: Vec<i64> = Vec::new();
v.push(1);
v.push(2);
v.push(3);
println(iter.any(v, |x: i64| x > 2));  // true
println(iter.any(v, |x: i64| x > 9));  // false

Function all

pub fn all(v: Vec<i64>, f: fn(i64) -> bool) -> bool

Return true if f returns true for every element.

Examples

let v: Vec<i64> = Vec::new();
v.push(1);
v.push(2);
v.push(3);
println(iter.all(v, |x: i64| x > 0));  // true
println(iter.all(v, |x: i64| x > 2));  // false

Function sum

pub fn sum(v: Vec<i64>) -> i64

Return the sum of all elements.

Examples

let v: Vec<i64> = Vec::new();
v.push(1);
v.push(2);
v.push(3);
println(iter.sum(v));  // 6

Function map_str

pub fn map_str(v: Vec<string>, f: fn(string) -> string) -> Vec<string>

Apply f to every element and return a new vector of results.

Examples

let v: Vec<string> = Vec::new();
v.push("hello");
v.push("world");
let shouted = iter.map_str(v, |s: string| s + "!");
// shouted == ["hello!", "world!"]

Function filter_str

pub fn filter_str(v: Vec<string>, f: fn(string) -> bool) -> Vec<string>

Return a new vector containing only elements for which f returns true.

Examples

let v: Vec<string> = Vec::new();
v.push("foo");
v.push("bar");
v.push("baz");
let bs = iter.filter_str(v, |s: string| s.starts_with("b"));
// bs == ["bar", "baz"]

Function fold_str

pub fn fold_str(v: Vec<string>, init: string, f: fn(string, string) -> string) -> string

Left-fold: accumulate all elements starting from init.

Examples

let v: Vec<string> = Vec::new();
v.push("a");
v.push("b");
v.push("c");
let joined = iter.fold_str(v, "", |acc: string, s: string| acc + s);
// joined == "abc"

Function any_str

pub fn any_str(v: Vec<string>, f: fn(string) -> bool) -> bool

Return true if f returns true for at least one element.

Examples

let v: Vec<string> = Vec::new();
v.push("foo");
v.push("bar");
println(iter.any_str(v, |s: string| s == "bar"));  // true
println(iter.any_str(v, |s: string| s == "baz"));  // false

Function all_str

pub fn all_str(v: Vec<string>, f: fn(string) -> bool) -> bool

Return true if f returns true for every element.

Examples

let v: Vec<string> = Vec::new();
v.push("foo");
v.push("bar");
println(iter.all_str(v, |s: string| s.len() == 3));  // true
println(iter.all_str(v, |s: string| s == "foo"));    // false

Function map_f64

pub fn map_f64(v: Vec<f64>, f: fn(f64) -> f64) -> Vec<f64>

Apply f to every element and return a new vector of results.

Examples

let v: Vec<f64> = Vec::new();
v.push(1.0);
v.push(2.0);
let doubled = iter.map_f64(v, |x: f64| x * 2.0);
// doubled == [2.0, 4.0]

Function filter_f64

pub fn filter_f64(v: Vec<f64>, f: fn(f64) -> bool) -> Vec<f64>

Return a new vector containing only elements for which f returns true.

Examples

let v: Vec<f64> = Vec::new();
v.push(1.0);
v.push(2.5);
v.push(3.0);
let big = iter.filter_f64(v, |x: f64| x > 2.0);
// big == [2.5, 3.0]

Function fold_f64

pub fn fold_f64(v: Vec<f64>, init: f64, f: fn(f64, f64) -> f64) -> f64

Left-fold: accumulate all elements starting from init.

Examples

let v: Vec<f64> = Vec::new();
v.push(1.0);
v.push(2.0);
v.push(3.0);
let total = iter.fold_f64(v, 0.0, |acc: f64, x: f64| acc + x);
// total == 6.0

Function any_f64

pub fn any_f64(v: Vec<f64>, f: fn(f64) -> bool) -> bool

Return true if f returns true for at least one element.

Examples

let v: Vec<f64> = Vec::new();
v.push(1.0);
v.push(2.0);
v.push(3.0);
println(iter.any_f64(v, |x: f64| x > 2.5));  // true
println(iter.any_f64(v, |x: f64| x > 9.0));  // false

Function all_f64

pub fn all_f64(v: Vec<f64>, f: fn(f64) -> bool) -> bool

Return true if f returns true for every element.

Examples

let v: Vec<f64> = Vec::new();
v.push(1.0);
v.push(2.0);
v.push(3.0);
println(iter.all_f64(v, |x: f64| x > 0.0));  // true
println(iter.all_f64(v, |x: f64| x > 2.0));  // false

Function sum_f64

pub fn sum_f64(v: Vec<f64>) -> f64

Return the sum of all elements.

Examples

let v: Vec<f64> = Vec::new();
v.push(1.0);
v.push(2.5);
v.push(3.0);
println(iter.sum_f64(v));  // 6.5

Function take_int

pub fn take_int(v: Vec<i64>, n: i64) -> Vec<i64>

Return the first n elements of v. If n exceeds the length, the whole vector is returned.

Examples

let v: Vec<i64> = Vec::new();
v.push(10); v.push(20); v.push(30);
let t = iter.take_int(v, 2);
// t == [10, 20]

Function skip_int

pub fn skip_int(v: Vec<i64>, n: i64) -> Vec<i64>

Skip the first n elements of v and return the rest. If n exceeds the length, an empty vector is returned.

Examples

let v: Vec<i64> = Vec::new();
v.push(10); v.push(20); v.push(30);
let s = iter.skip_int(v, 1);
// s == [20, 30]

Function count_int

pub fn count_int(v: Vec<i64>, f: fn(i64) -> bool) -> i64

Return the number of elements for which f returns true.

Examples

let v: Vec<i64> = Vec::new();
v.push(1); v.push(2); v.push(3); v.push(4);
println(iter.count_int(v, |x: i64| x % 2 == 0));  // 2

Function take_str

pub fn take_str(v: Vec<string>, n: i64) -> Vec<string>

Return the first n elements of v. If n exceeds the length, the whole vector is returned.

Examples

let v: Vec<string> = Vec::new();
v.push("a"); v.push("b"); v.push("c");
let t = iter.take_str(v, 2);
// t == ["a", "b"]

Function skip_str

pub fn skip_str(v: Vec<string>, n: i64) -> Vec<string>

Skip the first n elements of v and return the rest. If n exceeds the length, an empty vector is returned.

Examples

let v: Vec<string> = Vec::new();
v.push("a"); v.push("b"); v.push("c");
let s = iter.skip_str(v, 1);
// s == ["b", "c"]

Function count_str

pub fn count_str(v: Vec<string>, f: fn(string) -> bool) -> i64

Return the number of elements for which f returns true.

Examples

let v: Vec<string> = Vec::new();
v.push("foo"); v.push("bar"); v.push("baz");
println(iter.count_str(v, |s: string| s.starts_with("b")));  // 2

Function take_f64

pub fn take_f64(v: Vec<f64>, n: i64) -> Vec<f64>

Return the first n elements of v. If n exceeds the length, the whole vector is returned.

Examples

let v: Vec<f64> = Vec::new();
v.push(1.0); v.push(2.0); v.push(3.0);
let t = iter.take_f64(v, 2);
// t == [1.0, 2.0]

Function skip_f64

pub fn skip_f64(v: Vec<f64>, n: i64) -> Vec<f64>

Skip the first n elements of v and return the rest. If n exceeds the length, an empty vector is returned.

Examples

let v: Vec<f64> = Vec::new();
v.push(1.0); v.push(2.0); v.push(3.0);
let s = iter.skip_f64(v, 1);
// s == [2.0, 3.0]

Function count_f64

pub fn count_f64(v: Vec<f64>, f: fn(f64) -> bool) -> i64

Return the number of elements for which f returns true.

Examples

let v: Vec<f64> = Vec::new();
v.push(1.0); v.push(2.5); v.push(-1.0);
println(iter.count_f64(v, |x: f64| x > 0.0));  // 2

Function product

pub fn product(v: Vec<i64>) -> i64

Return the product of all elements. Returns 1 for an empty vector (the multiplicative identity).

Examples

let v: Vec<i64> = Vec::new();
v.push(2); v.push(3); v.push(4);
println(iter.product(v));  // 24

Function product_f64

pub fn product_f64(v: Vec<f64>) -> f64

Return the product of all elements. Returns 1.0 for an empty vector (the multiplicative identity).

Examples

let v: Vec<f64> = Vec::new();
v.push(2.0); v.push(0.5); v.push(4.0);
println(iter.product_f64(v));  // 4.0

Types

Struct Map

Lazy 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.

Fields

iter: I
f: fn(A) -> B

Struct Filter

Lazy filter adapter: yields only those items for which pred returns true. Inner Item and produced Item agree on A.

Fields

iter: I
pred: fn(A) -> bool

Struct Take

Lazy take adapter: yields at most remaining items from the inner iterator, then reports None.

Fields

iter: I
remaining: i64

Struct Skip

Lazy skip adapter: discards the first remaining items, then yields the rest of the inner iterator unchanged.

Fields

iter: I
remaining: i64