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.