Is there a shorter and simpler way to achieve this:
let o: Option<i32> = ...;
let ve: Vec<i32> = match o {
None => vec![],
Some(n) => vec![n],
};
Option implements IntoIterator, so you can make an iterator and collect it:
let ve: Vec<i32> = o.into_iter().collect();
let o: Option<i32> = Some(2);
let ve: Vec<i32> = o.into_iter().collect();
Playground Link
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With