fold_while

Function fold_while 

Source
pub fn fold_while<I, B, F>(it: I, init: B, f: F) -> B
where I: Iterator, F: FnMut(B, I::Item) -> (B, bool),
Expand description

Essentially a short-circuiting version of Iterator::fold() (see https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.fold). The only difference between this and Iterator::fold() is that the closure in this function returns two values: the accumulator value and a boolean. If the boolean returned is true, fold_while() will continue iterating, otherwise it will terminate early.

assert_eq!(
    // Sums items, but exits after item `3`.
    aoclib_rs::fold_while([1, 2, 3, 4, 5].iter(), 0, |acc, &item| (
        acc + item,
        item != 3
    )),
    6
);