Email me

Rust errors / Patterns and matching

Rust compiler error · checked with rustc 1.99.0

Rust error E0638: this error indicates that the struct, enum or enum variant must be matched non-exhaustively as it has been marked as `non_exhaustive`

What it meansThis error indicates that the struct, enum or enum variant must be matched non-exhaustively as it has been marked as non_exhaustive.

E0638Patterns and matching
Activein rustc 1.99.0
5related errors linked below

The compiler team's explanation

This error indicates that the struct, enum or enum variant must be matched non-exhaustively as it has been marked as non_exhaustive.

When applied within a crate, downstream users of the crate will need to use the _ pattern when matching enums and use the .. pattern when matching structs. Downstream crates cannot match against non-exhaustive enum variants.

For example, in the below example, since the enum is marked as non_exhaustive, it is required that downstream crates match non-exhaustively on it.

#[non_exhaustive]
pub enum Error {
    Message(String),
    Other,
}

impl Display for Error {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        // This will not error, despite being marked as non_exhaustive, as this
        // enum is defined within the current crate, it can be matched
        // exhaustively.
        let display = match self {
            Message(s) => s,
            Other => "other or unknown error",
        };
        formatter.write_str(display)
    }
}

An example of matching non-exhaustively on the above enum is provided below:

use mycrate::Error;

// This will not error as the non_exhaustive Error enum has been matched with a
// wildcard.
match error {
    Message(s) => ...,
    Other => ...,
    _ => ...,
}

Similarly, for structs, match with .. to avoid this error.

From rustc --explain E0638 (1.99.0), part of the Rust project, MIT or Apache-2.0. Official page.

Related errors

All 518 Rust error codes · Rust crate data · Glossary

Up next

Often hit nextE0004: non-exhaustive patternsA related error from the same family.Read it next →

Did this answer your question?

Get the receipts by email.

What my AI agents tried this week, with the real numbers: what worked, what flopped, what made money. One email when there is something worth reading.

No spam, never sold. Every email has a one-click unsubscribe.

Popular in the shop

All 124 tools →

Browse: Bundles and kitsHome, family and schoolMoney and businessAI toolsSports and fitnessMaking and hobbiesFood and drinkIndie dev and launchTrading and quant

Free: Landing page teardown iPhone app cost calculator LLM API cost calculator Paid online: $49 Launch Check App builds Articles FAQ