Skip to Content
E0004: Non-Exhaustive Patterns

error[E0004]: non-exhaustive patterns

Your match doesn’t handle every possible value. Rust requires matches to be exhaustive, and the message names a value you missed.

Quick fix: Add an arm for the missing pattern the compiler names, or finish with a wildcard _ => arm.

Code that triggers E0004

src/main.rs
enum Direction { North, East, South, West, } fn label(dir: Direction) -> &'static str { match dir { Direction::North => "up", Direction::East => "right", Direction::South => "down", } } fn main() { for dir in [Direction::North, Direction::East, Direction::South, Direction::West] { println!("{}", label(dir)); } }

What the compiler says

error[E0004]: non-exhaustive patterns: `Direction::West` not covered --> src/main.rs:9:11 | 9 | match dir { | ^^^ pattern `Direction::West` not covered | note: `Direction` defined here --> src/main.rs:1:6 | 1 | enum Direction { | ^^^^^^^^^ ... 5 | West, | ---- not covered = note: the matched value is of type `Direction` help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown | 12~ Direction::South => "down", 13~ Direction::West => todo!(), | error: aborting due to 1 previous error For more information about this error, try `rustc --explain E0004`.

Why Rust raises E0004

A match must cover every value of the type being matched. That guarantee is what makes match on enums so useful: when you add a variant, the compiler points at every match that needs updating. Here Direction::West has no arm.

How to fix E0004

1. Add the missing arm

Handle the variant the compiler lists. For enums you control, this is usually the best fix, because the next new variant will be caught the same way.

src/main.rs
enum Direction { North, East, South, West, } fn label(dir: Direction) -> &'static str { match dir { Direction::North => "up", Direction::East => "right", Direction::South => "down", Direction::West => "left", } } fn main() { for dir in [Direction::North, Direction::East, Direction::South, Direction::West] { println!("{}", label(dir)); } }

2. Use a wildcard for everything else

When you can’t list every value (numbers, strings), finish with a _ => arm. Use it sparingly with your own enums: a wildcard also hides the error you’d want when someone adds a new variant.

src/main.rs
fn describe(code: u16) -> &'static str { match code { 200 => "ok", 404 => "not found", 500..=599 => "server error", _ => "something else", } } fn main() { println!("{}", describe(418)); }

Learn the concept behind it

The Match and Pattern Matching lesson explains the rules behind E0004 with diagrams and more examples. For the official reference, run rustc --explain E0004 or read the E0004 entry in the Rust error index .

Hit a different error? Paste it into the Rust error lookup to find its fix.

Last updated on