03-绑定
绑定 — Rust By Example
译文 · 基于 Rust By Example
原文链接: https://doc.rust-lang.org/stable/rust-by-example/flow_control/match/binding.html
绑定
在 match 中,若间接地访问一个变量,则不经过重新绑定就无法在分支中再使用它。match 提供了 @ 符号来绑定变量到名称:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| // `age` 函数,返回一个 `u32` 值。
fn age() -> u32 {
15
}
fn main() {
println!("Tell me what type of person you are");
match age() {
0 => println!("I haven't celebrated my first birthday yet"),
// 可以直接匹配(`match`) 1 ..= 12,但那样的话孩子会是几岁?
// 相反,在 1 ..= 12 分支中绑定匹配值到 `n` 。现在年龄就可以读取了。
n @ 1 ..= 12 => println!("I'm a child of age {:?}", n),
n @ 13 ..= 19 => println!("I'm a teen of age {:?}", n),
// 不符合上面的范围。返回结果。
n => println!("I'm an old person of age {:?}", n),
}
}
|
你也可以使用绑定来“解构” enum 变体,例如 Option:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| fn some_number() -> Option<u32> {
Some(42)
}
fn main() {
match some_number() {
// 得到 `Some` 可变类型,如果它的值(绑定到 `n` 上)等于 42,则匹配。
Some(n @ 42) => println!("The Answer: {}!", n),
// 匹配任意其他数字。
Some(n) => println!("Not interesting... {}", n),
// 匹配任意其他值(`None` 可变类型)。
_ => (),
}
}
|
参见:
函数,枚举 和 Option