I am parsing JSON into structs using serde, and for one specific situation I am trying obtain a value or return "-", indicating no name (which are named 'Ni' or 'No' for None-inner and None-outer in the code below).
I am successful in doing so using the below nested match.
The question I have is if I can do this in a more compressed way possibly more idiomatic (such as serialising functions?
fn main() {
#[derive(Debug)]
struct InnerTest {
c: Option<String>
}
#[derive(Debug)]
struct Test {
a: String,
b: Option<InnerTest>,
}
let mut t: Vec<Test> = Vec::new();
t.push( Test { a: "a".to_string(), b: None });
t.push( Test { a: "aa".to_string(), b: Some(InnerTest { c: None }) });
t.push( Test { a: "aa".to_string(), b: Some(InnerTest { c: Some("c".to_string()) }) });
//println!("{:#?}", t);
// option 1: open
for tt in t {
let o = match tt.b {
Some(x) => {
match x.c {
Some(i) => i.to_string(),
None => String::from("Ni")
}
},
None => String::from("No"),
};
println!("{}", o);
}
}