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
if let
just doesn't support multiple patterns (see RFC issue 935). Use match
instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With