Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple patterns not working with if let

Tags:

rust

You can use if let to pattern match on a range:

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

but I can't make it work on multiple patterns:

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

I thought the second if let desugared to:

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

so what gives? Am I using the wrong syntax? Is my expectation that multiple patterns should work just misguided? Is this just not implemented?

playground

like image 591
Paolo Falabella Avatar asked Aug 25 '17 08:08

Paolo Falabella


1 Answers

if let just doesn't support multiple patterns (see RFC issue 935). Use match instead.

like image 139
DK. Avatar answered Oct 02 '22 19:10

DK.