Skip to content

Commit

Permalink
Update with slice pattern restrictions
Browse files Browse the repository at this point in the history
  • Loading branch information
workingjubilee committed Sep 12, 2021
1 parent da75d92 commit 7168679
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions src/patterns.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
>    | [_IdentifierPattern_]\
>    | [_WildcardPattern_]\
>    | [_RestPattern_]\
>    | [_ObsoleteRangePattern_]\
>    | [_ReferencePattern_]\
>    | [_StructPattern_]\
>    | [_TupleStructPattern_]\
Expand Down Expand Up @@ -401,7 +400,14 @@ match tuple {

> **<sup>Syntax</sup>**\
> _RangePattern_ :\
> &nbsp;&nbsp; &nbsp;&nbsp; _RangePatternBound_ `..=` _RangePatternBound_\
> &nbsp;&nbsp; &nbsp;&nbsp; _InclusiveRangePattern_\
> &nbsp;&nbsp; | _HalfOpenRangePattern_\
> &nbsp;&nbsp; | _ObsoleteRangePattern_
> _InclusiveRangePattern_ :\
> &nbsp;&nbsp; &nbsp;&nbsp; _RangePatternBound_ `..=` _RangePatternBound_
>
> _HalfOpenRangePattern_ :\
> &nbsp;&nbsp; | _RangePatternBound_ `..`
>
> _ObsoleteRangePattern_ :\
Expand All @@ -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 &le; 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 &le; b. It is an error to have a range pattern
`10..=0`, for example.

The `...` syntax is kept for backwards compatibility.
Expand Down Expand Up @@ -708,7 +716,11 @@ match int_reference {
> &nbsp;&nbsp; `[` _SlicePatternItems_<sup>?</sup> `]`
>
> _SlicePatternItems_ :\
> &nbsp;&nbsp; [_Pattern_] \(`,` [_Pattern_])<sup>\*</sup> `,`<sup>?</sup>
> &nbsp;&nbsp; [_InSlicePattern_] \(`,` [_InSlicePattern_])<sup>\*</sup> `,`<sup>?</sup>
>
> _InSlicePattern_ :\
> &nbsp;&nbsp; &nbsp;&nbsp; _PatternWithoutRange_\
> &nbsp;&nbsp; | [_ClosedRangePattern_]
Slice patterns can match both arrays of fixed size and slices of dynamic size.
```rust
Expand Down Expand Up @@ -762,6 +774,7 @@ refer to refutable constants or enum variants for enums with multiple variants.

[_GroupedPattern_]: #grouped-patterns
[_IdentifierPattern_]: #identifier-patterns
[_InSlicePattern_]: #slice-patterns
[_LiteralPattern_]: #literal-patterns
[_MacroInvocation_]: macros.md#macro-invocation
[_ObsoleteRangePattern_]: #range-patterns
Expand All @@ -771,6 +784,7 @@ refer to refutable constants or enum variants for enums with multiple variants.
[_PatternWithoutRange_]: #patterns
[_QualifiedPathInExpression_]: paths.md#qualified-paths
[_RangePattern_]: #range-patterns
[_ClosedRangePattern_]: #range-patterns
[_ReferencePattern_]: #reference-patterns
[_RestPattern_]: #rest-patterns
[_SlicePattern_]: #slice-patterns
Expand Down

0 comments on commit 7168679

Please sign in to comment.