BreakOn iterator

My idea is to develop a break-on iterator.

How should it work? Let’s start with an example:

let x: Vec<i32> = (0..9)
                    .break_on(|&i| i == 5)
                    .collect();
assert_eq!(x, [0, 1, 2, 3, 4]);

BreakOn may be a wrapper (such as Map) that calls a predicate on each item, sets flag to true if predicate returns true, and returns None if flag is true. That’s how it may look like (pseudocode):

struct BreakOn<T: Iterator> {
    iter: T,
    predicate: fn(T::Item) -> bool,
    flag: bool,
}

There are several details that I did not notice to let the community discuss this (or criticize).

Oh i feel so stupid. Thanks anyway.

This is Iterator in std::iter - Rust.

5 Likes
assert_eq!((0..10).scan(0,|s,x|if x==5 {None} else {Some(x)}).collect::<Vec<_>>(),[0, 1, 2, 3, 4]);

Maybe, no need to implement such iterator

@notriddle, can you please close this one? =|

(I replied accidentally)