FAQ - Help and questions

How do I fix "non-exhaustive patterns" in a match (E0004)?

#errors#enums#pattern-matchingpublished 2 replies0 views
ReplyWatch
Share
Hacker NewsRedditLinkedInE-mail
Last post

Short answer

A match must cover every possible value. The message names the case you left out. Add an arm for it, or end with a wildcard arm _ that covers the rest. Prefer listing the cases for your own enums, so the compiler reminds you when a new variant is added later.

Checked with rustc 1.93.0, Windows 11

Frequently asked question, written and answered by the Ruststudio team. Replies are open: corrections are welcome.

#1

Answer#

match checks at compile time that no case can slip through, which is the reason enums are safe to extend: add a variant and every match on it that is not updated stops compiling.

src/main.rsrust
enum Light {
    Red,
    Yellow,
    Green,
}

fn name(l: Light) -> &'static str {
    match l {
        Light::Red => "stop",
        Light::Green => "go",
    }
}

fn main() {
    println!("{}", name(Light::Yellow));
}
text
error[E0004]: non-exhaustive patterns: `Light::Yellow` not covered
 --> src/main.rs:8:11
  |
8 |     match l {
  |           ^ pattern `Light::Yellow` not covered
  |
note: `Light` defined here
 --> src/main.rs:1:6
  |
1 | enum Light {
  |      ^^^^^
2 |     Red,
3 |     Yellow,
  |     ------ not covered
  = note: the matched value is of type `Light`
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
  |
10~         Light::Green => "go",
11~         Light::Yellow => todo!(),
  |

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0004`.

The answers below are the two valid fixes.

    QuoteReply
    Share
    Hacker NewsRedditLinkedInE-mail

    2 answers from the Ruststudio team: different approaches, each with its own trade-offs

    Sort: Oldest Newest
    #2

    Approach 1: add the missing arm#

    Write out the case. This is the better choice for your own enums, because adding a variant later gives you a new error at every match that must decide what to do.

    src/main.rsrust
    enum Light {
        Red,
        Yellow,
        Green,
    }
    
    fn name(l: Light) -> &'static str {
        match l {
            Light::Red => "stop",
            Light::Yellow => "slow down",
            Light::Green => "go",
        }
    }
    
    fn main() {
        println!("{}", name(Light::Yellow));
    }
    text
    slow down
      QuoteReply
      Share
      Hacker NewsRedditLinkedInE-mail
      #3

      Approach 2: use a wildcard arm#

      _ => ... matches everything not named before it. It is the only choice for numbers and strings, which have too many values to list.

      rust
      fn size(n: u32) -> &'static str {
          match n {
              0 => "empty",
              1..=9 => "small",
              _ => "large",
          }
      }

      Place _ last: arms are tried in order, and anything after it is unreachable.

      Common mistakes#

      • Using _ on your own enum "to make it compile". You lose the reminder that a new variant needs handling.
      • Using todo!() as the arm body and forgetting it. It panics at run time when that case occurs.
      • Reaching for if let when only one case matters. if let Light::Red = l { ... } needs no other arms.
        QuoteReply
        Share
        Hacker NewsRedditLinkedInE-mail