您可以使用if let模式匹配范围:
let n=1
if let 1...3 = n { println!("found in range") }
Run Code Online (Sandbox Code Playgroud)
但我不能让它适用于多种模式:
// this does not compile
if let 1 | 2 | 3 = n { println!("found in pattern") }
// -^ unexpected token
Run Code Online (Sandbox Code Playgroud)
我以为第二个if let去了:
// this does compile and work
match n {
1 | 2 | 3 => println!("found in pattern"),
_ => {}
}
Run Code Online (Sandbox Code Playgroud)
什么赋予了什么?我使用了错误的语法吗?我是否期望多种模式应该被误导?这只是没有实现?