Skip to content

Pattern matching with guards does not allow multiple matches. #551

Closed
@viesturz

Description

@viesturz

Hi,

I want to match multiple patterns with guards to a single code block.
I was wondering if there is any reason why the following code does not compile?

let b = (3i,4i);
let g = 0i;
match b {
   (2,_) | (_, 2) => println!("2 inside "), // this works fine
   (2,_) if g > 0 => println!("First is 2 and some guards"), // also works fine
   (2,_) if g > 0 | (_, 2) => println!("2 inside and some guards"), // this does not compile
   ....
}

Also have you considered allowing to use a general if condition syntax in matches, like this?

   ((2,_) && g > 0) || (_, 2) => 

Edit:

Also this does compile, but looks a bit ambiguous to what part of the pattern the guard applies to.

(2,_) | (_, 2) if g > 0 => println!("2 inside and some filters"),

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-langRelevant to the language team, which will review and decide on the RFC.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions