Function unfold
fn unfold<A, St, F>(initial_state: St, f: F) -> Unfold<St, F>
where
F: FnMut(&mut St) -> Option<A>
Creates a new unfold source with the specified closure as the "iterator function" and an initial state to eventually pass to the closure
unfold is a general iterator builder: it has a mutable state value,
and a closure with access to the state that produces the next value.
This more or less equivalent to a regular struct with an Iterator
implementation, and is useful for one-off iterators.
// an iterator that yields sequential Fibonacci numbers,
// and stops at the maximum representable value.
use unfold;
let mut fibonacci = unfold;
assert_equal;
assert_eq!