Email me

Rust errors / Patterns and matching

Rust compiler error · checked with rustc 1.99.0

Rust error E0004: non-exhaustive patterns

What it meansA match does not handle every possible value. Add the missing arms or a wildcard _.

What rustc prints

error[E0004]: non-exhaustive patterns: `Terminator::HastaLaVistaBaby` not covered
  --> main.rs:10:7
   |
10 | match x { // error: non-exhaustive patterns: `HastaLaVistaBaby` not covered
   |       ^ pattern `Terminator::HastaLaVistaBaby` not covered
   |
note: `Terminator` defined here
  --> main.rs:3:6
   |
 3 | enum Terminator {
   |      ^^^^^^^^^^
 4 |     HastaLaVistaBaby,
   |     ---------------- not covered
   = note: the matched value is of type `Terminator`
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
   |
11 ~     Terminator::TalkToMyHand => {},
12 +     Terminator::HastaLaVistaBaby => todo!()
   |

Real output of rustc 1.99.0 compiling the official example below (2021 edition). Your names and line numbers will differ; the first line is the part to match.

Why it happens

  • A new enum variant was added.
  • Matching on integers or strings without a catch-all.

How to fix it

1. Add the missing arm

match dir {
    Dir::Up => 1,
    Dir::Down => -1,
    _ => 0,
}

Other wordings of this error

non-exhaustive patterns, patterns not covered, ensure that all possible cases are being handled

E0004Patterns and matching
Activein rustc 1.99.0
2related errors linked below

The compiler team's explanation

This error indicates that the compiler cannot guarantee a matching pattern for one or more possible inputs to a match expression. Guaranteed matches are required in order to assign values to match expressions, or alternatively, determine the flow of execution.

Erroneous code example:

enum Terminator {
    HastaLaVistaBaby,
    TalkToMyHand,
}

let x = Terminator::HastaLaVistaBaby;

match x { // error: non-exhaustive patterns: `HastaLaVistaBaby` not covered
    Terminator::TalkToMyHand => {}
}

If you encounter this error you must alter your patterns so that every possible value of the input type is matched. For types with a small number of variants (like enums) you should probably cover all cases explicitly. Alternatively, the underscore _ wildcard pattern can be added after all other patterns to match "anything else". Example:

enum Terminator {
    HastaLaVistaBaby,
    TalkToMyHand,
}

let x = Terminator::HastaLaVistaBaby;

match x {
    Terminator::TalkToMyHand => {}
    Terminator::HastaLaVistaBaby => {}
}

// or:

match x {
    Terminator::TalkToMyHand => {}
    _ => {}
}

From rustc --explain E0004 (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 nextE0023: this pattern has 1 field, but the corresponding tuple variant has 2 fieldsA 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