Skip to content

Rust的match表达式 #5

Description

@stephen7812

let number = 42;
match number {
// 模式为单个值
0 => println!("Origin"),
// 模式为Range**(这里的1...3要改成1..=3 才行,不然会报错。)**
1...3 => println!("All"),
// 模式为 多个值
| 5 | 7 | 13 => println!("Bad Luck"),
// 绑定模式,将模式中的值绑定给一个变量,供右边执行代码使用
n @ 42 => println!("Answer is {}", n),
// _ 通配符处理剩余情况
_ => println!("Common"),
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions