[][src]Function tao_of_rust::ch02::control_flow::match_expr

pub fn match_expr()

match 匹配

Basic usage:

fn match_expr() {
    let number = 42;
    match number {
        0 => println!("Origin"),  // 匹配数字
        1...3 => println!("All"), // 匹配范围
        | 5 | 7 | 13 => println!("Bad Luck"), // 匹配相同的分支
        n @ 42 => println!("Answer is {}", n), // 使用@可以创建绑定n,分支右侧表达式中可用
        _ => println!("Common"),  // 下划线为通用匹配
    }
}
match_expr();Run