Home › Rust

error[E0004]: non-exhaustive patterns (Rust) — how to fix

error[E0004]: non-exhaustive patterns: `Direction::West` not covered
 --> src/main.rs:9:11
  |
9 |     match dir {
  |           ^^^ pattern `Direction::West` not covered

Quick fix: Add an arm for each value the compiler lists as not covered, or add a catch-all _ => ... arm at the end. For Option/Result handle both Some/None (Ok/Err); for integers and strings you almost always need _.

What it means

In Rust, a match must cover every value its input could have. The compiler checks this and lists the patterns you missed. This is a feature: when you add a new enum variant later, every match that doesn't handle it stops compiling until you decide what to do.

Cause 1: an enum variant isn't handled

enum Direction { North, East, South, West }

fn turn(dir: Direction) -> &'static str {
    match dir {
        Direction::North => "up",
        Direction::East => "right",
        Direction::South => "down",
    }   // E0004: `Direction::West` not covered
}

Add the arm:

        Direction::West => "left",

Or group variants with |:

        Direction::East | Direction::West => "sideways",

Prefer listing variants explicitly over _ for your own enums, so the compiler flags new variants later.

Cause 2: Option or Result with one side missing

match map.get("key") {
    Some(v) => println!("{v}"),
}   // E0004: `None` not covered

Handle the other case, or use a construct that doesn't require it:

match map.get("key") {
    Some(v) => println!("{v}"),
    None => println!("missing"),
}

if let Some(v) = map.get("key") {
    println!("{v}");
}

let Some(v) = map.get("key") else {
    return;          // let-else (Rust 1.65+): must diverge
};

Cause 3: matching on numbers, chars or strings

Integer and string types have too many values to list, so any match on them needs a catch-all:

match code {
    200 => "ok",
    404 => "not found",
    _ => "other",            // required
}

match cmd.as_str() {
    "start" => start(),
    "stop" => stop(),
    other => eprintln!("unknown command: {other}"),
}

Ranges count too: the compiler can verify 0..=9 | 10..=u8::MAX covers all of u8, but for signed integers remember the negative values (i32::MIN..=-1).

Cause 4: slices and tuples

match items.as_slice() {
    [] => println!("empty"),
    [one] => println!("one: {one}"),
}   // E0004: `&[_, _, ..]` not covered

Add a rest pattern:

    [first, rest @ ..] => println!("{first} and {} more", rest.len()),

For tuples, missing combinations are listed, e.g. (true, false) not covered. Add those arms or a _ arm.

Cause 5: #[non_exhaustive] enums from other crates

Some library enums (for example std::io::ErrorKind) are marked #[non_exhaustive], meaning more variants may be added later. Outside the defining crate you must include a wildcard even if you listed every current variant:

match err.kind() {
    ErrorKind::NotFound => ...,
    ErrorKind::PermissionDenied => ...,
    _ => ...,
}

let Some(x) = opt; gives error[E0005]: refutable pattern in local binding for the same reason: None isn't covered. Use if let, let ... else, or match.

Last updated 2026-12-03.