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.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.
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.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')
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.
@ (at) operator. This captures the matched value into a local variable while simultaneously enforcing the boundary constraints of the range pattern.
Master Rust with Deep Grasping Methodology!Learn More





