Skip to content

Commit 8865a78

Browse files
authored
Merge pull request rust-lang#435 from ehuss/pattern-group
Add grouped patterns.
2 parents b63782c + e140f59 commit 8865a78

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

src/patterns.md

+27-6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
>    | [_StructPattern_]\
1111
>    | [_TupleStructPattern_]\
1212
>    | [_TuplePattern_]\
13+
>    | [_GroupedPattern_]\
1314
>    | [_SlicePattern_]\
1415
>    | [_PathPattern_]
1516
@@ -582,6 +583,25 @@ They are also used to [destructure](#destructuring) a tuple.
582583

583584
This pattern is refutable when one of its subpatterns is refutable.
584585

586+
## Grouped patterns
587+
588+
> **<sup>Syntax</sup>**\
589+
> _GroupedPattern_ :\
590+
> &nbsp;&nbsp; `(` [_Pattern_] `)`
591+
592+
Enclosing a pattern in parentheses can be used to explicitly control the
593+
precedence of compound patterns. For example, a reference pattern next to a
594+
range pattern such as `&0..=5` is ambiguous and is not allowed, but can be
595+
expressed with parentheses.
596+
597+
```rust
598+
let int_reference = &3;
599+
match int_reference {
600+
&(0..=5) => (),
601+
_ => (),
602+
}
603+
```
604+
585605
## Slice patterns
586606

587607
> **<sup>Syntax</sup>**\
@@ -633,17 +653,18 @@ Path patterns are irrefutable when they refer to structs or an enum variant when
633653
has only one variant or a constant whose type is irrefutable. They are refutable when they
634654
refer to refutable constants or enum variants for enums with multiple variants.
635655

636-
[_Pattern_]: #patterns
656+
[_GroupedPattern_]: #grouped-patterns
657+
[_IdentifierPattern_]: #identifier-patterns
637658
[_LiteralPattern_]: #literal-patterns
638-
[_WildcardPattern_]: #wildcard-pattern
659+
[_PathPattern_]: #path-patterns
660+
[_Pattern_]: #patterns
639661
[_RangePattern_]: #range-patterns
640662
[_ReferencePattern_]: #reference-patterns
641-
[_IdentifierPattern_]: #identifier-patterns
642-
[_TupleStructPattern_]: #tuple-struct-patterns
663+
[_SlicePattern_]: #slice-patterns
643664
[_StructPattern_]: #struct-patterns
644665
[_TuplePattern_]: #tuple-patterns
645-
[_SlicePattern_]: #slice-patterns
646-
[_PathPattern_]: #path-patterns
666+
[_TupleStructPattern_]: #tuple-struct-patterns
667+
[_WildcardPattern_]: #wildcard-pattern
647668

648669
[`Copy`]: special-types-and-traits.html#copy
649670
[IDENTIFIER]: identifiers.html

0 commit comments

Comments
 (0)