You can use filter_map
it only returns the Somes not the Nones.
let optvec = vec![Some(1), None, Some(4), None];let filtered: Vec<i32> = optvec.iter().filter_map(|f| *f).collect();println!("{:?}", filtered);>>> [1, 4]
You can use filter_map
it only returns the Somes not the Nones.
let optvec = vec![Some(1), None, Some(4), None];let filtered: Vec<i32> = optvec.iter().filter_map(|f| *f).collect();println!("{:?}", filtered);>>> [1, 4]