Pattern Matching

The following examples present pattern matching using the match keyword for the catch-all case.

Encouraged

The _ is used for the catch-all to indicate the important cases have been defined above and the last case is not important enough to warrant a name.

fn unnamed_case(shape: Shape) {
    let value = match shape {
        Shape::Triangle => 3,
        Shape::Quadrilateral => 4,
        Shape::Pentagon => 5,
        _ => 0,
    };
}

Alternative

We may apply an appropriate name to provide context to the reader; however, unless it provides additional information the preferred usage is defined in the encouraged case.

fn named_case(shape: Shape) {
    let value = match shape {
        Shape::Triangle => 3,
        Shape::Quadrilateral => 4,
        Shape::Pentagon => 5,
        _invalid_shape => 0,
    };
}