- TODO
Filter vector of structs without copy
examples/vectors/filter-vector-of-structs/src/main.rs
#[derive(Debug)] #[allow(dead_code)] struct Something { number: i32, text: String, numbers: Vec<i32>, } fn main() { let va: Vec<Something> = vec![ Something { number: 1, text: String::from("small"), numbers: vec![1, 2, 3], }, Something { number: 11, text: String::from("medium"), numbers: vec![11, 12], }, Something { number: 101, text: String::from("large"), numbers: vec![101], }, ]; println!("{:#?}", va); //let vb = &va.iter().collect::<Vec<&Something>>(); let vb = &va.iter().collect::<Vec<_>>(); let v_big = &vb .iter() .filter(|thing| thing.number > 20) .collect::<Vec<_>>(); //let v_big = &vb.into_iter().filter(|thing| thing.number > 20).collect::<Vec<_>>(); println!("{:#?}", v_big); println!("{:#?}", vb); }
[ Something { number: 1, text: "small", numbers: [ 1, 2, 3, ], }, Something { number: 11, text: "medium", numbers: [ 11, 12, ], }, Something { number: 101, text: "large", numbers: [ 101, ], }, ] [ Something { number: 101, text: "large", numbers: [ 101, ], }, ] [ Something { number: 1, text: "small", numbers: [ 1, 2, 3, ], }, Something { number: 11, text: "medium", numbers: [ 11, 12, ], }, Something { number: 101, text: "large", numbers: [ 101, ], }, ]