For the past few months, I’ve been studying Rust.
retain is a standard library method on Vec<T>, and others, that acts as a filter.
This one is in place: no new allocation.
It preserves order and runs in O(n).
Under the hood it shifts the kept elements down over the removed ones and truncates once at the end. Much cheaper than removing elements one by one with remove(), which is O(n) per removal.