What Is Exhaustive Pattern Matching in Rust?

Exhaustive pattern matching is a Rust compiler rule requiring all enum variants to be handled in a match expression to prevent unhandled runtime errors.

Exhaustive pattern matching in Rust is a compiler requirement that forces you to handle every possible variant of an enum or data structure in a match expression. If you miss a case, the code will not compile, preventing runtime errors from unhandled states.

enum IpAddrKind {
    V4,
    V6,
}

fn route(ip_kind: IpAddrKind) {
    match ip_kind {
        IpAddrKind::V4 => println!("V4"),
        IpAddrKind::V6 => println!("V6"),
    }
}