2017-08-25 40 views
6

工作,就可以使用if let模式匹配一​​个范围:多种模式不若让

let n=1 
if let 1...3 = n { println!("found in range") } 

,但我不能让它在多模式工作:

// this does not compile 
if let 1 | 2 | 3 = n { println!("found in pattern") } 
//  -^ unexpected token 

我以为第二个if let desugared:

// this does compile and work 
match n { 
    1 | 2 | 3 => println!("found in pattern"), 
    _ => {} 
} 

那么是什么给?我使用错误的语法吗?我期望多种模式应该起作用吗?这只是没有实施?

playground

+1

https://github.com/rust-lang/rfcs/issues/935 – interjay

+0

@interjay啊谢谢,所以它只是没有实现。 github问题确实提到了多种模式,但是后面的讨论集中在了看守方面。出于某种原因(丑陋的结果语法?)我不那么惊讶,如果让守卫不工作。范围工作与多种模式不起作用似乎有点不直观,我... –

回答

8

if let只是不支持多模式(见RFC issue 935)。改为使用match