卫语句

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

  1. fn main() {
  2. let pair = (2, -2);
  3. // 试一试 ^ 将不同的值赋给 `pair`
  4. println!("Tell me about {:?}", pair);
  5. match pair {
  6. (x, y) if x == y => println!("These are twins"),
  7. // ^ `if` 条件部分是一个卫语句
  8. (x, y) if x + y == 0 => println!("Antimatter, kaboom!"),
  9. (x, _) if x % 2 == 1 => println!("The first one is odd"),
  10. _ => println!("No correlation..."),
  11. }
  12. }

参见:

元组