Pattern matching with match for exhaustive case handling

Marcus Chen Jan 2026
1 tab
enum Status {
    Ok,
    Error(String),
    Pending,
}

fn handle_status(status: Status) -> String {
    match status {
        Status::Ok => "Success".to_string(),
        Status::Error(msg) => format!("Failed: {}", msg),
        Status::Pending => "Waiting...".to_string(),
    }
}
1 file · rust Explain with highlit

Rust's match expression is like a switch statement on steroids. It requires exhaustive handling of all cases, and the compiler enforces this. You can match on enums, tuples, references, ranges, and destructure nested data. Guards (if condition) add extra filtering. The _ wildcard catches everything else. Because match is an expression, it returns a value, making code more concise. I use match for state machines, parsing, and any branching logic with multiple cases. The exhaustiveness check catches bugs at compile time when you add new enum variants. Combined with pattern destructuring, it's one of Rust's most powerful features for writing correct code.