Skip to content

RFC: exhaustive matching idiom #694

@dtolnay

Description

@dtolnay

Here is the pattern I would like to document and recommend for use cases that require exhaustive matching (which is uncommon, but they exist):

match expr {
    Expr::Array(e) => {...}
    Expr::Assign(e) => {...}
    ...
    Expr::Yield(e) => {...}

    #[cfg(test)]
    Expr::TestExhaustive => unimplemented!(),
    #[cfg(not(test))]
    _ => { /* some sane fallback */ }
}

This way we fail your tests but don't break your library when adding a variant. I believe this has all the benefits of an exhaustive enum and all the benefits of a nonexhaustive enum for only a little effort.

FYI @zerakun @phaylon @BurntSushi from the reddit thread.
FYI @mystor for brainstorming.
FYI @Centril because language support for nonexhaustive would need to have at least these benefits before I would want to pick it up.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions