is it possible to filter on a vector in-place?

If you want to remove elements, you can use retain(), which removes elements from the vector if the closure returns false:

let mut vec = vec![1, 2, 3, 4];
vec.retain(|&x| x % 2 == 0);
assert_eq!(vec, [2, 4]);

If you want to modify the elements in place, you have to do that in a for x in vec.iter_mut().

Leave a Comment