Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

filter string

  • iter
  • filter
  • cloned
  • collect
fn main() {
    let animals: Vec<String> = vec![
        String::from("elephant"),
        String::from("cat"),
        String::from("snake"),
        String::from("dog"),
    ];
    println!("{:?}", &animals);

    let same_animals: Vec<String> = animals.iter().filter(|_animal| true).cloned().collect();
    println!("{:?}", &same_animals);

    let short_animals: Vec<String> = animals
        .iter()
        .filter(|animal| animal.len() < 4)
        .cloned()
        .collect();
    println!("{:?}", &short_animals);
}
["elephant", "cat", "snake", "dog"]
["elephant", "cat", "snake", "dog"]
["cat", "dog"]