[][src]Function im::iter::unfold_mut

Important traits for UnfoldMut<F, S>
#[must_use]
pub fn unfold_mut<F, S, A>(value: S, f: F) -> UnfoldMut<F, S> where
    F: Fn(&mut S) -> Option<A>, 

Create an iterator of values using a function to mutate a state value.

The function is called with a mutable reference to the current state as its argument, and should return an Option of the next value to yield from the iterator, updating the state as necessary. If the function returns None, the iterator ends.

This differs from unfold in that your update functions will probably be less elegant, but it's easier to deal with state that isn't efficiently cloneable.

Examples

// Create an infinite stream of numbers, starting at 0.
let mut it = unfold_mut(0, |i| {
  let next = *i;
  *i += 1;
  Some(next)
});

// Make a list out of its first five elements.
let numbers = Vector::from_iter(it.take(5));
assert_eq!(numbers, vector![0, 1, 2, 3, 4]);