RUST 学习日记 第14课 ——if match

57 阅读1分钟

1 match

match 匹配,_ 代表通配符

let some_u8_value = 0u8;
match some_u8_value {
  1 => println!("one"), 
  3 => println!("three"),
  5 => println!("five"), 
  7 => println!("seven"), 
  _ => (),
}

2 match 枚举匹配

除了_通配符,用一个变量来承载其他情况也可以

#[derive(Debug)]
enum Direction{
East,
West,
North,
South,
}

fn main(){
  let dire = Direction::South;
  match dire {
    Direction::East => println!("East"),
    other => println!("other direction: {:?}",other),
  }
}

if let匹配

只有一个模式的值需要被处理

let v = Some(3u8);
match v {
  Some(3) => println!("three"),
  _=> (),
}