Function im::iter::unfold
[−]
[src]
ⓘImportant traits for Unfold<F, S>
pub fn unfold<F, S, A>(value: S, f: F) -> Unfold<F, S> where
F: Fn(&S) -> Option<(A, S)>,
Create an iterator of values using a function to update a state value.
The function is called with the current state as its argument, and
should return an Option
of a tuple of the
next value to yield from the iterator and the updated state. If
the function returns None
, the
iterator ends.
Examples
// Create an infinite stream of numbers, starting at 0. let mut it = unfold(0, |i| Some((*i, *i + 1))); // Make a list out of its first five elements. let numbers = CatList::from_iter(it.take(5)); assert_eq!(numbers, catlist![0, 1, 2, 3, 4]);