How to Use drain and retain on Collections in Rust

Use drain to remove and iterate over items, and retain to filter a collection in place based on a condition.

Use drain to remove and iterate over items while keeping the collection, and retain to keep only items matching a condition.

let mut workers = vec![1, 2, 3];

// Drain all items to process and remove them
for worker in workers.drain(..) {
    println!("Shutting down worker {}", worker);
}

// Retain only even numbers
let mut numbers = vec![1, 2, 3, 4];
numbers.retain(|&x| x % 2 == 0);
// numbers is now [2, 4]