-
Notifications
You must be signed in to change notification settings - Fork 0
/
match_binding.rs
33 lines (29 loc) · 1.04 KB
/
match_binding.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
// A function `age` which returns a `u32`.
fn age() -> u32 {
15
}
fn some_number() -> Option<u32> {
Some(42)
}
pub fn play() {
println!("Tell me what type of person you are");
match age() {
0 => println!("I'm not born yet I guess"),
// Could `match` 1 ..= 12 directly but then what age
// would the child be? Instead, bind to `n` for the
// sequence of 1 ..= 12. Now the age can be reported.
n @ 1 ..= 12 => println!("I'm a child of age {:?}", n),
n @ 13 ..= 19 => println!("I'm a teen of age {:?}", n),
// Nothing bound. Return the result.
n => println!("I'm an old person of age {:?}", n),
}
match some_number() {
// Got `Some` variant, match if its value, bound to `n`,
// is equal to 42.
Some(n @ 42) => println!("The Answer: {}!", n),
// Match any other number.
Some(n) => println!("Not interesting... {}", n),
// Match anything else (`None` variant).
_ => (),
}
}