Skip to content

Unclear match behaviour when using multiple patterns and a guard #26012

Closed
@jonhoo

Description

@jonhoo

Consider the following code:

fn main() {
    enum E {
        A,
        B,
    }

    let x = E::A;
    let y = true;

    match x {
        E::A | E::B if !y => println!("if applies only to last pattern"),
        _ => println!("if applies to all OR'd patterns"),
    }
}

The behaviour of this code depends on whether the guard applies to only the pattern it succeeds, or to the entire match arm. Running it on nightly behaves as though the guard applies to the entire arm (i.e. the first arm is not matched), but this is not stipulated anywhere in the docs as far as I can tell. If this interpretation of the code is indeed the expected one, then this should be documented.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions