Range Patterns Cover Match Arms

Fold consecutive exit codes into named bands with range patterns: literal ranges like 64..=78 become match arms, and char ranges classify tokens too.

RustCli Tooling

Code

rustsnippet
27 lines
fn classify_exit(code: i32) -> &'static str {
    // Range patterns fold a consecutive span into one arm; uncovered values fall to the _ wildcard
    match code {
        0 => "success",
        1 => "generic failure",
        2 => "cli usage error",
        64..=78 => "sysexits convention",
        126 => "cannot execute",
        127 => "command not found",
        _ => "unclassified",
    }
}

fn main() {
    for code in [0, 2, 69, 127, 42] {
        println!("{code}: {}", classify_exit(code));
    }

    // Char ranges work the same way: inclusive ..= composes with or-patterns
    let token = 'q';
    let kind = match token {
        'a'..='z' | 'A'..='Z' => "letter",
        '0'..='9' => "digit",
        _ => "other",
    };
    println!("{token}: {kind}");
}

Inside the snippet

The working surface is the functions classify_exit and main — everything else is wiring.

Iteration happens inside for code in [0, 2, 69, 127, 42] — everything outside it runs once. Branching goes through match, whose exhaustiveness checking turns forgotten cases into compile errors.

Putting it to use

Entry signature: fn classify_exit(code: i32) -> &'static str

The entry point wires classify_exit together — run it once and the whole demo flows.

Core lines

let kind = match token {
    'a'..='z' | 'A'..='Z' => "letter",
    '0'..='9' => "digit",
    _ => "other",
};
match code {
    0 => "success",
    1 => "generic failure",
    2 => "cli usage error",
    64..=78 => "sysexits convention",
    126 => "cannot execute",
    127 => "command not found",
    _ => "unclassified",
}

Interactive Tools for This Topic

FAQ

How do you run Range Patterns Cover Match Arms?
This is a complete program, not a library excerpt — execution starts at the top.