pattern

Rust Bind a value matched by a range

A range pattern tests a value against bounds; an at-binding also preserves the matched value under a name. Use this when a category needs to retain the original number. Expected output (not executed): high: 87 Why: 87 is inside the inclusive range and becomes points. Common mistake: A range pattern is not an arbitrary runtime comparison; use a guard for dynamic bounds.

Formal Syntax & Structure

rust syntaxrust
name @ lower..=upper

Working Example

Example Snippetrust
fn main() {    let score = 87;    match score {        points @ 80..=100 => println!("high: {points}"),        _ => println!("other"),    }}
Rust Reference