I'm learning Rust and am trying to get used to working with Result
s and Option
s. Given a vector of either. If i only want the results that did not err (or aren't none for Option
), is there a more graceful way of doing this than below or is this about the amount of boilerplate i'd have to usually write?
I realize there are a lot more granular that one can do in map
like unwrap_or_else
and partition
good results away from bad.
let optvec = vec![Some(1), None, Some(4), None]; let filtered = optvec.iter() .filter(|q| q.is_some()) .map(|q| q.unwrap()) .collect::<Vec<i32>>();