Ad Space — Top Banner

E0004

Rust Programming Language

Severity: Moderate

What Does This Error Mean?

Your match expression does not handle every possible value. Rust requires match to be exhaustive — every case must be covered. Add the missing patterns or use a wildcard catch-all arm.

Affected Models

  • Rust stable
  • Rust nightly
  • Cargo build
  • Rust 2021 edition

Common Causes

  • A match on an enum that is missing one or more variant arms
  • A new variant was added to an enum but not all match expressions were updated
  • Matching on a boolean but only handling true, not false
  • Matching on an integer range that does not cover all possible values
  • Using a non-exhaustive enum from an external crate that added a new variant

How to Fix It

  1. Add the missing pattern arms that TypeScript reports.

    The error message lists the patterns that are not covered. Add an arm for each one.

  2. Add a wildcard arm (_ =>) to catch all remaining cases.

    Example: _ => {} catches everything not explicitly matched. Use this when the remaining cases all have the same behaviour.

  3. Add a catch-all arm that panics or logs if an unexpected value appears.

    Example: _ => panic!('Unexpected variant') makes logic errors loud instead of silent.

  4. If the enum is from an external crate, use a wildcard to handle future variants.

    External enums may add variants in new versions. A wildcard arm protects against future compilation failures.

  5. Consider using if let instead of match if you only care about one specific variant.

    if let Some(val) = my_option { ... } handles one case without needing to cover all others explicitly.

When to Call a Professional

E0004 is always safe to fix yourself — it is a compile-time error. When a new enum variant causes many match errors across a large codebase, a team review ensures each case is handled correctly.

Frequently Asked Questions

Why does Rust require exhaustive matching?

Rust's match must handle every possible value — this is a language guarantee. If you add a new enum variant, every match on that enum must be updated. This prevents the silent 'fell through' bugs common in switch statements in other languages.

What is a non-exhaustive enum in Rust?

The #[non_exhaustive] attribute on an enum signals that more variants may be added in the future. Code outside the crate that defines it must use a wildcard arm in match. This is used by library authors to reserve the right to add variants without a breaking change.

Can I match on multiple patterns in one arm?

Yes — use | to separate multiple patterns in a single arm. Example: Coin::Penny | Coin::Nickel => println!('small coin') This reduces repetition when several variants share the same behaviour.