How to filter a Vec

Filter a Rust Vec by chaining .into_iter().filter(|item| condition).collect() to create a new vector containing only matching elements.

Use the filter method on the vector's iterator with a closure that returns true for items you want to keep, then call collect() to create a new vector.

let filtered: Vec<Shoe> = shoes
    .into_iter()
    .filter(|s| s.size == 10)
    .collect();

This pattern works for any condition, such as checking if a string contains a substring:

let results: Vec<&str> = contents
    .lines()
    .filter(|line| line.contains(query))
    .collect();