Skip to content

Commit 2ae480f

Browse files
Update with slice pattern restrictions
1 parent da75d92 commit 2ae480f

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

src/patterns.md

+19-6
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
>    | [_IdentifierPattern_]\
1111
>    | [_WildcardPattern_]\
1212
>    | [_RestPattern_]\
13-
>    | [_ObsoleteRangePattern_]\
1413
>    | [_ReferencePattern_]\
1514
>    | [_StructPattern_]\
1615
>    | [_TupleStructPattern_]\
@@ -401,7 +400,14 @@ match tuple {
401400

402401
> **<sup>Syntax</sup>**\
403402
> _RangePattern_ :\
404-
> &nbsp;&nbsp; &nbsp;&nbsp; _RangePatternBound_ `..=` _RangePatternBound_\
403+
> &nbsp;&nbsp; &nbsp;&nbsp; _InclusiveRangePattern_\
404+
> &nbsp;&nbsp; | _HalfOpenRangePattern_\
405+
> &nbsp;&nbsp; | _ObsoleteRangePattern_
406+
>
407+
> _InclusiveRangePattern_ :\
408+
> &nbsp;&nbsp; &nbsp;&nbsp; _RangePatternBound_ `..=` _RangePatternBound_
409+
>
410+
> _HalfOpenRangePattern_ :\
405411
> &nbsp;&nbsp; | _RangePatternBound_ `..`
406412
>
407413
> _ObsoleteRangePattern_ :\
@@ -421,12 +427,14 @@ it matches all the values between and including both of its bounds. A range patt
421427
half-open is written with a lower bound but not an upper bound, and matches any value equal to
422428
or greater than the specified lower bound.
423429

424-
For example, a pattern `'m'..='p'` will match only the values `'m'`, `'n'`, `'o'`, and `'p'`. The
430+
For example, a pattern `'m'..='p'` will match only the values `'m'`, `'n'`, `'o'`, and `'p'`. For an integer the
425431
pattern `1..` will match 9, or 9001, or 9007199254740991 (if it is of an appropriate size), but
426-
not 0 or negative numbers for signed integers. The bounds can be literals or paths that point
432+
not 0, and not negative numbers for signed integers. The bounds can be literals or paths that point
427433
to constant values.
428434

429-
A pattern a `..=` b must always have a &le; b. It is an error to have a range pattern
435+
A half-open range pattern in the style `a..` cannot be used to match within the context of a slice.
436+
437+
A pattern `a..=b` must always have a &le; b. It is an error to have a range pattern
430438
`10..=0`, for example.
431439

432440
The `...` syntax is kept for backwards compatibility.
@@ -708,7 +716,11 @@ match int_reference {
708716
> &nbsp;&nbsp; `[` _SlicePatternItems_<sup>?</sup> `]`
709717
>
710718
> _SlicePatternItems_ :\
711-
> &nbsp;&nbsp; [_Pattern_] \(`,` [_Pattern_])<sup>\*</sup> `,`<sup>?</sup>
719+
> &nbsp;&nbsp; _InSlicePattern_ \(`,` _InSlicePattern_)<sup>\*</sup> `,`<sup>?</sup>
720+
>
721+
> _InSlicePattern_ :\
722+
> &nbsp;&nbsp; &nbsp;&nbsp; [_PatternWithoutRange_]\
723+
> &nbsp;&nbsp; | [_ClosedRangePattern_]
712724
713725
Slice patterns can match both arrays of fixed size and slices of dynamic size.
714726
```rust
@@ -771,6 +783,7 @@ refer to refutable constants or enum variants for enums with multiple variants.
771783
[_PatternWithoutRange_]: #patterns
772784
[_QualifiedPathInExpression_]: paths.md#qualified-paths
773785
[_RangePattern_]: #range-patterns
786+
[_ClosedRangePattern_]: #range-patterns
774787
[_ReferencePattern_]: #reference-patterns
775788
[_RestPattern_]: #rest-patterns
776789
[_SlicePattern_]: #slice-patterns

0 commit comments

Comments
 (0)