1818>   ;  ; [ _ OuterAttribute_ ] <sup >\* </sup > _ MatchArmPatterns_ _ MatchArmGuard_ <sup >?</sup >
1919>
2020> _ MatchArmPatterns_ :\
21- >   ;  ; ` | ` <sup >?</sup > _ Pattern_ ( ` | ` _ Pattern_ )<sup >\* </sup >
21+ >   ;  ; ` | ` <sup >?</sup > [ _ Pattern_ ] ( ` | ` [ _ Pattern_ ] )<sup >\* </sup >
2222>
2323> _ MatchArmGuard_ :\
2424>   ;  ; ` if ` [ _ Expression_ ]
2525
2626A * ` match ` expression* branches on a pattern. The exact form of matching that
27- occurs depends on the pattern. * Patterns* consist of some combination of
28- literals, destructured arrays or enum constructors, structs and tuples,
29- variable binding specifications, wildcards (` .. ` ), and placeholders (` _ ` ). A
30- ` match ` expression has a * head expression* , which is the value to compare to
31- the patterns. The type of the patterns must equal the type of the head
32- expression.
27+ occurs depends on the [ pattern] . A ` match `
28+ expression has a * head expression* , which is the value to compare to the
29+ patterns. The head expression and the patterns must have the same type.
3330
3431A ` match ` behaves differently depending on whether or not the head expression
3532is a [ place expression or value expression] [ place expression ] .
@@ -62,65 +59,10 @@ match x {
6259}
6360```
6461
65- Patterns that bind variables default to binding to a copy or move of the
66- matched value (depending on the matched value's type). This can be changed to
67- bind to a reference by using the ` ref ` keyword, or to a mutable reference using
68- ` ref mut ` .
62+ Variables bound within the pattern are scoped to the match guard and the arm's
63+ expression. The [ binding mode] (move, copy, or reference) depends on the pattern.
6964
70- Patterns can be used to * destructure* structs, enums, and tuples. Destructuring
71- breaks a value up into its component pieces. The syntax used is the same as
72- when creating such values. When destructing a data structure with named (but
73- not numbered) fields, it is allowed to write ` fieldname ` as a shorthand for
74- ` fieldname: fieldname ` . In a pattern whose head expression has a ` struct ` ,
75- ` enum ` or ` tupl ` type, a placeholder (` _ ` ) stands for a * single* data field,
76- whereas a wildcard ` .. ` stands for * all* the fields of a particular variant.
77-
78- ``` rust
79- # enum Message {
80- # Quit ,
81- # WriteString (String ),
82- # Move { x : i32 , y : i32 },
83- # ChangeColor (u8 , u8 , u8 ),
84- # }
85- # let message = Message :: Quit ;
86- match message {
87- Message :: Quit => println! (" Quit" ),
88- Message :: WriteString (write ) => println! (" {}" , & write ),
89- Message :: Move { x , y : 0 } => println! (" move {} horizontally" , x ),
90- Message :: Move { .. } => println! (" other move" ),
91- Message :: ChangeColor { 0 : red , 1 : green , 2 : _ } => {
92- println! (" color change, red: {}, green: {}" , red , green );
93- }
94- };
95- ```
96-
97- Patterns can also dereference pointers by using the ` & ` , ` &mut ` and ` box `
98- symbols, as appropriate. For example, these two matches on ` x: &i32 ` are
99- equivalent:
100-
101- ``` rust
102- let int_reference = & 3 ;
103-
104- let a = match * int_reference { 0 => " zero" , _ => " some" };
105- let b = match int_reference { & 0 => " zero" , _ => " some" };
106-
107- assert_eq! (a , b );
108- ```
109-
110- Subpatterns can also be bound to variables by the use of the syntax `variable @
111- subpattern`. For example:
112-
113- ``` rust
114- let x = 1 ;
115-
116- match x {
117- e @ 1 ... 5 => println! (" got a range element {}" , e ),
118- _ => println! (" anything" ),
119- }
120- ```
121-
122- Multiple match patterns may be joined with the ` | ` operator. An inclusive range
123- of values may be specified with ` ..= ` . For example:
65+ Multiple match patterns may be joined with the ` | ` operator:
12466
12567``` rust
12668# let x = 9 ;
@@ -133,34 +75,11 @@ let message = match x {
13375assert_eq! (message , " a few" );
13476```
13577
136- Other forms of [ range] \( e.g ` .. ` for an exclusive range, or any range with one or
137- both endpoints left unspecified) are not supported in matches. The
138- syntax ` ... ` is also accepted for inclusive ranges in patterns only, for
139- backwards compatibility.
140-
141- Range patterns only work with [ ` char ` ] and [ numeric types] . A range pattern may
142- not be a sub-range of another range pattern inside the same ` match ` .
143-
144- Slice patterns can match both arrays of fixed size and slices of dynamic size.
145- ``` rust
146- // Fixed size
147- let arr = [1 , 2 , 3 ];
148- match arr {
149- [1 , _ , _ ] => " starts with one" ,
150- [a , b , c ] => " starts with something else" ,
151- };
152- ```
153- ``` rust
154- // Dynamic size
155- let v = vec! [1 , 2 , 3 ];
156- match v [.. ] {
157- [a , b ] => { /* this arm will not apply because the length doesn't match */ }
158- [a , b , c ] => { /* this arm will apply */ }
159- _ => { /* this wildcard is required, since we don't know length statically */ }
160- }
161- ```
78+ Please notice that the ` 2..=9 ` is a [ Range Pattern] , not a [ Range Expression]
79+ and, thus, only those types of ranges supported by range patterns can be used
80+ in match arms.
16281
163- Finally, match arms can accept * pattern guards * to further refine the
82+ Match arms can accept _ match guards _ to further refine the
16483criteria for matching a case. Pattern guards appear after the pattern and
16584consist of a bool-typed expression following the ` if ` keyword. A pattern guard
16685may refer to the variables bound within the pattern they follow.
@@ -205,10 +124,17 @@ meaning on match arms are [`cfg`], `cold`, and the [lint check attributes].
205124[_BlockExpression_ ]: expressions / block - expr . html#block - expressions
206125[place expression ]: expressions . html#place - expressions - and - value - expressions
207126[value expression ]: expressions . html#place - expressions - and - value - expressions
208- [`char `]: types . html#textual - types
209- [numeric types ]: types . html#numeric - types
210127[_InnerAttribute_ ]: attributes . html
211128[_OuterAttribute_ ]: attributes . html
212129[`cfg `]: conditional - compilation . html
213130[lint check attributes ]: attributes . html#lint - check - attributes
214- [range ]: expressions / range - expr . html
131+ [Range Expression ]: expressions / range - expr . html
132+
133+ [_Pattern_ ]: patterns . html
134+ [pattern ]: patterns . html
135+ [Identifier Patterns ]: patterns . html#identifier - patterns
136+ [Struct Patterns ]: patterns . html#struct - patterns
137+ [Tuple Struct Patterns ]: patterns . html#tuplestruct - patterns
138+ [Tuple Patterns ]: patterns . html#tuple - patterns
139+ [Range Pattern ]: patterns . html#range - patterns
140+ [binding mode ]: patterns . html#binding - modes
0 commit comments