Function itertools::iterate

source ·
pub fn iterate<St, F>(initial_value: St, f: F) -> Iterate<St, F> where
    F: FnMut(&St) -> St,
Expand description

Creates a new iterator that infinitely applies function to value and yields results.

use itertools::iterate;

itertools::assert_equal(iterate(1, |i| i % 3 + 1).take(5), vec![1, 2, 3, 1, 2]);

Panics if compute the next value does.

let mut it = iterate(25u32, |x| x - 10).take_while(|&x| x > 10);
assert_eq!(it.next(), Some(25)); // `Iterate` holds 15.
assert_eq!(it.next(), Some(15)); // `Iterate` holds 5.
it.next(); // `5 - 10` overflows.

You can alternatively use core::iter::successors as it better describes a finite iterator.