Given a Vec<i32>
I’d like to create a Vec<i32>
containing all first i32
s with a predicate returning true dropping all elements once the first element not satisfying the predicate is encountered. The simple filter implementation as
fn first_predicate<F>(vec: Vec<i32>, f: F) -> Vec<i32>
where
F: FnMut(&i32) -> bool,
{
vec.into_iter().filter(f).collect() //return all elements satisfying the predicate, not the first group
}
as it returns all the elements satisfying the predicate.
Is there a way to stop iteration once an element not satisfying the predicate is encountered?