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.
Code
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
Fuzzy Match TesterTest subsequence fuzzy matching: check whether pattern characters appear in order inside a text, with positions and highlighting.Git Conventional Commit BuilderBuild and validate Conventional Commits v1.0.0 messages with type, scope, breaking markers, 72-char subject counter, body, footers, and git CLI command.Glob Pattern TesterCompile glob patterns with *, **, ?, character classes, and braces into regex and test them against a list of candidate file paths.SemVer Range CheckerCheck whether a semantic version satisfies an npm-style range such as caret, tilde, comparison sets, wildcards, or hyphen ranges.
FAQ
- How do you run Range Patterns Cover Match Arms?
- This is a complete program, not a library excerpt — execution starts at the top.
Report a problem with this exampleOpens a pre-filled issue on GitHub.