Quantcast
Channel: Idiomatic way to only get insides of `Some` from a vector of options? - Stack Overflow
Viewing all articles
Browse latest Browse all 3

Idiomatic way to only get insides of `Some` from a vector of options?

$
0
0

I'm learning Rust and am trying to get used to working with Results and Options. 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>>();

Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles





Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>
<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596344.js" async> </script>