守卫

可以加上 match 守卫(guard) 来过滤分支。

1. fn main() {
2. let pair = (2, -2);
3. // 试一试 ^ 将不同的值赋给 `pair`

5. println!("Tell me about {:?}", pair);
6. match pair {
7. (x, y) if x == y => println!("These are twins"),
8. // ^ `if condition`(if 条件)部分是一个守卫
9. (x, y) if x + y == 0 => println!("Antimatter, kaboom!"),
10. (x, _) if x % 2 == 1 => println!("The first one is odd"),
11. _ => println!("No correlation..."),
12. }
13. }

参见:

Tuples