pub fn drain_filter<T>(
vector: &mut Vec<T>,
predicate: impl FnMut(&T) -> bool,
) -> Vec<T>
Expand description
Drain and filter at the same time, accepts a mutable vector ref
and predicate
.
The predicate
receives a vector element ref
for the current iteration and
in-place removes the element from the vector, if predicate
returns true
.
Returns and array of removed
elements back.
ยงExamples
use cs_utils::{drain_filter, random_bool};
let mut test_vector = vec![
random_bool(),
random_bool(),
random_bool(),
random_bool(),
random_bool(),
random_bool(),
random_bool(),
random_bool(),
random_bool(),
random_bool(),
random_bool(),
];
let removed_items = drain_filter(
&mut test_vector,
|el| {
return *el;
},
);
assert!(
!removed_items.contains(&false),
"Removed items must not contain \"false\" values.",
);
assert!(
!test_vector.contains(&true),
"Removed items must not contain \"true\" values.",
);