Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A range pattern is a pattern-matching construct that evaluates whether a target value falls within a contiguous sequence of scalar values defined by a lower and upper bound. It is evaluated at runtime as a set of boolean boundary checks rather than a discrete sequence of equality comparisons.

Syntax and Bounds

Range patterns utilize Rust’s standard range operators to define boundaries. Stable Rust fully supports both inclusive and exclusive ranges, as well as half-open bounds.
const MIN_VAL: i32 = 10;
const MAX_VAL: i32 = 20;
let target_value = 15;

match target_value {
    // Half-open exclusive upper bound (matches < -10)
    ..-10 => {} 

    // Half-open inclusive upper bound (matches <= 0)
    ..=0 => {}

    // Closed exclusive range (matches MIN_VAL through MAX_VAL - 1)
    MIN_VAL..MAX_VAL => {}

    // Closed inclusive range using literals (matches 30 through 39)
    30..=39 => {}

    // Half-open lower bound (matches >= 50)
    50.. => {}
    
    _ => {}
}

Technical Constraints

Constant Bounds Range pattern bounds must be constant expressions. This strictly requires the use of literals (e.g., 42, 'a') or explicitly declared const variables. Dynamically evaluated runtime variables (standard local variables) cannot be used as bounds; attempting to do so will result in a compile-time error regarding non-constant values in patterns (such as error[E0158] or error[E0435]). Type Restrictions Range patterns are strictly limited to scalar types that possess a defined, contiguous order at compile time. This restricts their usage to:
  • Integer types (i8, u8, i32, usize, etc.)
  • Character types (char)
  • Byte literals (b'a')
Note: Floating-point types (f32, f64) are not supported in range patterns due to the ambiguity of floating-point representation and NaN comparisons. Exhaustiveness Checking When utilized within a match expression, range patterns do not inherently satisfy the compiler’s exhaustiveness requirements unless the bounds explicitly cover the absolute minimum and maximum values of the target type (e.g., u8::MIN..=u8::MAX). In almost all scenarios, a wildcard pattern (_) or binding is required to handle values outside the specified ranges. Evaluation Order and Shadowing Like all patterns in Rust, range patterns are evaluated strictly top-to-bottom. If a range pattern overlaps with a preceding pattern, the first matching arm is executed. The Rust compiler’s pattern-matching analysis pass will emit an unreachable_pattern warning if a range pattern is entirely subsumed by a previous arm.
let value = 7;

match value {
    0..=10 => { /* Evaluated first: value >= 0 && value <= 10 */ }
    5..=15 => { /* Evaluated second: value >= 5 && value <= 15 */ } // 5..=10 is shadowed
    _ => { /* Fallback */ }
}
Binding Integration Range patterns can be bound to variables using the @ (at) operator. This captures the matched value into a local variable while simultaneously enforcing the boundary constraints of the range pattern.
let target_value = 42;

match target_value {
    bound_var @ 0..=100 => { 
        // bound_var holds the value (42) because it falls in the range 
    }
    _ => {}
}
Master Rust with Deep Grasping Methodology!Learn More