Created
July 31, 2018 22:24
-
-
Save rbuckland/80c28974b0b2f4ff735972b13ffdc9b9 to your computer and use it in GitHub Desktop.
Get an Option<Vec<_>> from Option<struct: Option<Vec>>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate serde_json; | |
#[derive(Debug)] | |
struct Foo { | |
bar: Option<Vec<&'static str>> | |
} | |
fn show(f: Option<Foo>) { | |
let v = f.and_then(|f1| f1.bar); | |
println!("vec == {:?}", v); | |
} | |
fn main() { | |
let bah = Some(Foo{ bar: Some(vec!["ddd","ggg","hhh"])}); | |
let bah2 = Some(Foo{ bar: None }); | |
let bah3 = None; | |
show(bah); | |
show(bah2); | |
show(bah3); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment