keyword

Rust match: choose a branch by pattern

Use `match` when a value may have several distinct cases. Rust checks that the patterns cover all possibilities. A wildcard `_` can handle values not selected by earlier arms. Expected output: Weekend Why: The value 6 matches the second arm. Common mistake: Leaving out the wildcard would leave some integer values unhandled.

Formal Syntax & Structure

rust syntaxrust
match value { pattern => expression, _ => expression }

Working Example

Example Snippetrust
fn main() {    let day = 6;    let label = match day {        1..=5 => "Workday",        6 | 7 => "Weekend",        _ => "Invalid day",    };    println!("{label}");}
Rust Reference