From d845bc3c28194c66357a8dde0ef5eed2e2f8db8a Mon Sep 17 00:00:00 2001 From: Jubilee Young Date: Sun, 8 Nov 2020 17:52:40 -0800 Subject: [PATCH 1/5] Add RangeFrom patterns --- src/patterns.md | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/patterns.md b/src/patterns.md index a27489fc7..763633c85 100644 --- a/src/patterns.md +++ b/src/patterns.md @@ -402,6 +402,8 @@ match tuple { > **Syntax**\ > _RangePattern_ :\ >    _RangePatternBound_ `..=` _RangePatternBound_ +> _RangeFromPattern_ :\ +>    _RangePatternBound_ `..` > > _ObsoleteRangePattern_ :\ >    _RangePatternBound_ `...` _RangePatternBound_ @@ -414,9 +416,10 @@ 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 that are within a range defined by its lower and upper bounds (if any). +For example, a pattern `'m'..='p'` will match only the values `'m'`, `'n'`, `'o'`, and `'p'`. A +pattern `1..` will match values equal to or greater than 1, but not 0 (or negative numbers, for +signed integers). The bounds can be literals or paths that point to constant values. A pattern a `..=` b must always have a ≤ b. It is an error to have a range pattern `10..=0`, for example. From abcacb2ce47e163e16ba1d7ec8d020ad6629a8d3 Mon Sep 17 00:00:00 2001 From: Jubilee Young Date: Thu, 25 Feb 2021 13:43:17 -0800 Subject: [PATCH 2/5] More formal writing and links --- src/patterns.md | 15 ++++++++++----- src/tokens.md | 3 ++- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/patterns.md b/src/patterns.md index 763633c85..35f2b030e 100644 --- a/src/patterns.md +++ b/src/patterns.md @@ -402,7 +402,6 @@ match tuple { > **Syntax**\ > _RangePattern_ :\ >    _RangePatternBound_ `..=` _RangePatternBound_ -> _RangeFromPattern_ :\ >    _RangePatternBound_ `..` > > _ObsoleteRangePattern_ :\ @@ -416,10 +415,16 @@ match tuple { >    | [_PathInExpression_]\ >    | [_QualifiedPathInExpression_] -Range patterns match values that are within a range defined by its lower and upper bounds (if any). -For example, a pattern `'m'..='p'` will match only the values `'m'`, `'n'`, `'o'`, and `'p'`. A -pattern `1..` will match values equal to or greater than 1, but not 0 (or negative numbers, for -signed integers). 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'`. The +pattern `1..` will match 9, or 9001, or 9007199254740991 (if it is of an appropriate size), but +not 0 or negative numbers for signed integers. The bounds can be literals or paths that point +to constant values. A pattern a `..=` b must always have a ≤ b. It is an error to have a range pattern `10..=0`, for example. 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 From 51fab18a424ca774bf1c8f352db7caed18e75a03 Mon Sep 17 00:00:00 2001 From: Jubilee Young Date: Tue, 6 Apr 2021 15:32:53 -0700 Subject: [PATCH 3/5] Offer example --- src/patterns.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/patterns.md b/src/patterns.md index 35f2b030e..d00c270e9 100644 --- a/src/patterns.md +++ b/src/patterns.md @@ -458,6 +458,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; From da75d920c5db7462c38cfb58e5dce7e9b9b3cec9 Mon Sep 17 00:00:00 2001 From: Jubilee <46493976+workingjubilee@users.noreply.github.com> Date: Tue, 3 Aug 2021 00:29:26 -0700 Subject: [PATCH 4/5] add spacing in src/patterns.md Co-authored-by: Eric Huss --- src/patterns.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/patterns.md b/src/patterns.md index d00c270e9..0a0af6707 100644 --- a/src/patterns.md +++ b/src/patterns.md @@ -401,8 +401,8 @@ match tuple { > **Syntax**\ > _RangePattern_ :\ ->    _RangePatternBound_ `..=` _RangePatternBound_ ->    _RangePatternBound_ `..` +>       _RangePatternBound_ `..=` _RangePatternBound_\ +>    | _RangePatternBound_ `..` > > _ObsoleteRangePattern_ :\ >    _RangePatternBound_ `...` _RangePatternBound_ From 76ed5db1e1783e8ab2642e6c69f596381126052d Mon Sep 17 00:00:00 2001 From: Jubilee Young Date: Sun, 12 Sep 2021 13:52:01 -0700 Subject: [PATCH 5/5] Update with slice pattern restrictions --- src/patterns.md | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/patterns.md b/src/patterns.md index 0a0af6707..3c7b324b4 100644 --- a/src/patterns.md +++ b/src/patterns.md @@ -10,7 +10,6 @@ >    | [_IdentifierPattern_]\ >    | [_WildcardPattern_]\ >    | [_RestPattern_]\ ->    | [_ObsoleteRangePattern_]\ >    | [_ReferencePattern_]\ >    | [_StructPattern_]\ >    | [_TupleStructPattern_]\ @@ -401,7 +400,14 @@ match tuple { > **Syntax**\ > _RangePattern_ :\ ->       _RangePatternBound_ `..=` _RangePatternBound_\ +>       _InclusiveRangePattern_\ +>    | _HalfOpenRangePattern_\ +>    | _ObsoleteRangePattern_ +> +> _InclusiveRangePattern_ :\ +>       _RangePatternBound_ `..=` _RangePatternBound_ +> +> _HalfOpenRangePattern_ :\ >    | _RangePatternBound_ `..` > > _ObsoleteRangePattern_ :\ @@ -421,12 +427,14 @@ it matches all the values between and including both of its bounds. A range patt 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'`. The +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 or negative numbers for signed integers. The bounds can be literals or paths that point +not 0, and not negative numbers for signed integers. The bounds can be literals or paths that point to constant values. -A pattern a `..=` b must always have a ≤ b. It is an error to have a range pattern +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 `10..=0`, for example. The `...` syntax is kept for backwards compatibility. @@ -734,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**\