-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatching.rs
70 lines (61 loc) · 1.48 KB
/
matching.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#![allow(dead_code)]
struct Inner {
i_label: String,
i_num: i32,
i_opt: Option<String>,
}
struct Middle {
m_label: String,
m_num: i32,
m_inner: Inner,
}
struct Outer {
o_label: String,
o_num: i32,
o_middle: Middle,
}
fn get_labels(o: &Outer) -> (&String, &String, &String) {
let Outer {
o_label: outer_label,
o_middle:
Middle {
m_label: middle_label,
m_inner:
Inner {
i_label: inner_label,
..
},
..
},
..
} = o;
(outer_label, middle_label, inner_label)
}
fn get_inner_opt(o: &Outer) -> &str {
match o.o_middle.m_inner.i_opt.as_ref() {
Some(v) => v.as_str(),
None => "",
}
}
pub fn main() {
let mut o = Outer {
o_label: "The Outer Struct".to_string(),
o_num: 42,
o_middle: Middle {
m_label: "The Middle Struct".to_string(),
m_num: 1728,
m_inner: Inner {
i_label: "The Inner Struct".to_string(),
i_num: 720,
i_opt: None,
},
},
};
let labels = get_labels(&o);
println!("LABELS: {:?}", labels);
let i_opt = get_inner_opt(&o);
println!("I OPT UNSET: {}", i_opt);
o.o_middle.m_inner.i_opt = Some(String::from("foo"));
let i_opt = get_inner_opt(&o);
println!("I OPT IS SET: {}", i_opt);
}