Closed
Description
Right now, you can do this
enum Foo {
One(u8),
Two(u8),
Three
}
use Foo::*;
fn main () {
let x = One(42);
match x {
One(n) | Two(n) => {
println!("Got one or two with val: {}", n);
}
_ => {}
}
}
but not this
enum Foo {
One(u8),
Two(u8),
Three
}
use Foo::*;
fn main () {
let x = One(42);
if let One(n) | Two(n) = x {
println!("Got one or two with val: {}", n);
}
}
The same goes for pattern guards.
The RFC for if-let proposes the if-let
notation pretty much as syntactic sugar for a match block with an empty arm at the end. However, if-let
doesn't allow matching on pattern guards and multiple patterns. Extending the if-let
notation to allow guards and multiple patterns feel like a natural extension to the current functionality with plenty of use cases.
I've started on an actual rfc, but there might be some parsing and syntactic details we'd have to figure out before it makes sense to finish it. For instance, if let Some(n) = x if n = 0
looks weird at best.
On the positive side, adding support for guards and multiple patterns shouldn't break anything.
cc @Manishearth