Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rbuckland/80c28974b0b2f4ff735972b13ffdc9b9 to your computer and use it in GitHub Desktop.
Save rbuckland/80c28974b0b2f4ff735972b13ffdc9b9 to your computer and use it in GitHub Desktop.
Get an Option<Vec<_>> from Option<struct: Option<Vec>>
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