diff --git a/src/patterns.md b/src/patterns.md index a27489fc7..3c7b324b4 100644 --- a/src/patterns.md +++ b/src/patterns.md @@ -10,7 +10,6 @@ >    | [_IdentifierPattern_]\ >    | [_WildcardPattern_]\ >    | [_RestPattern_]\ ->    | [_ObsoleteRangePattern_]\ >    | [_ReferencePattern_]\ >    | [_StructPattern_]\ >    | [_TupleStructPattern_]\ @@ -401,7 +400,15 @@ match tuple { > **Syntax**\ > _RangePattern_ :\ ->    _RangePatternBound_ `..=` _RangePatternBound_ +>       _InclusiveRangePattern_\ +>    | _HalfOpenRangePattern_\ +>    | _ObsoleteRangePattern_ +> +> _InclusiveRangePattern_ :\ +>       _RangePatternBound_ `..=` _RangePatternBound_ +> +> _HalfOpenRangePattern_ :\ +>    | _RangePatternBound_ `..` > > _ObsoleteRangePattern_ :\ >    _RangePatternBound_ `...` _RangePatternBound_ @@ -414,11 +421,20 @@ match tuple { >    | [_PathInExpression_]\ >    | [_QualifiedPathInExpression_] -Range patterns match values that are within the closed range defined by its lower and -upper bounds. For example, a pattern `'m'..='p'` will match only the values `'m'`, `'n'`, -`'o'`, and `'p'`. The bounds can be literals or paths that point to constant values. +Range patterns match values within the range defined by their bounds. A range pattern may be +closed or half-open. A range pattern is closed if it has both a lower and an upper bound, and +it matches all the values between and including both of its bounds. A range pattern that is +half-open is written with a lower bound but not an upper bound, and matches any value equal to +or greater than the specified lower bound. + +For example, a pattern `'m'..='p'` will match only the values `'m'`, `'n'`, `'o'`, and `'p'`. For an integer the +pattern `1..` will match 9, or 9001, or 9007199254740991 (if it is of an appropriate size), but +not 0, and not negative numbers for signed integers. The bounds can be literals or paths that point +to constant values. + +A half-open range pattern in the style `a..` cannot be used to match within the context of a slice. -A pattern a `..=` b must always have a ≤ b. It is an error to have a range pattern +A pattern `a..=b` must always have a ≤ b. It is an error to have a range pattern `10..=0`, for example. The `...` syntax is kept for backwards compatibility. @@ -450,6 +466,12 @@ println!("{}", match ph { _ => unreachable!(), }); +# let uint: u32 = 5; +match uint { + 0 => "zero!", + 1.. => "positive number!", +}; + // using paths to constants: # const TROPOSPHERE_MIN : u8 = 6; # const TROPOSPHERE_MAX : u8 = 20; @@ -720,6 +742,10 @@ is irrefutable. When matching a slice, it is irrefutable only in the form with a single `..` [rest pattern](#rest-patterns) or [identifier pattern](#identifier-patterns) with the `..` rest pattern as a subpattern. +Within a slice, a half-open range pattern like `a..` must be enclosed in parentheses, +as in `(a..)`, to clarify it is intended to match a single value. +A future version of Rust may give the non-parenthesized version an alternate meaning. + ## Path patterns > **Syntax**\ diff --git a/src/tokens.md b/src/tokens.md index f329ce912..ba431303e 100644 --- a/src/tokens.md +++ b/src/tokens.md @@ -586,7 +586,7 @@ usages and meanings are defined in the linked pages. | `@` | At | [Subpattern binding] | `_` | Underscore | [Wildcard patterns], [Inferred types], Unnamed items in [constants], [extern crates], and [use declarations] | `.` | Dot | [Field access][field], [Tuple index] -| `..` | DotDot | [Range][range], [Struct expressions], [Patterns] +| `..` | DotDot | [Range][range], [Struct expressions], [Patterns], [Range Patterns][rangepat] | `...` | DotDotDot | [Variadic functions][extern], [Range patterns] | `..=` | DotDotEq | [Inclusive Range][range], [Range patterns] | `,` | Comma | Various separators @@ -646,6 +646,7 @@ them are referred to as "token trees" in [macros]. The three types of brackets [patterns]: patterns.md [question]: expressions/operator-expr.md#the-question-mark-operator [range]: expressions/range-expr.md +[rangepat]: patterns.md#range-patterns [raw pointers]: types/pointer.md#raw-pointers-const-and-mut [references]: types/pointer.md [sized]: trait-bounds.md#sized