23
23
> _ MatchArmGuard_ :\
24
24
>   ;  ; ` if ` [ _ Expression_ ]
25
25
26
- A ` match ` expression branches on a * pattern* . The exact form of matching that
27
- occurs depends on the pattern. Patterns consist of some combination of
26
+ A * ` 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
28
literals, destructured arrays or enum constructors, structs and tuples,
29
29
variable binding specifications, wildcards (` .. ` ), and placeholders (` _ ` ). A
30
30
` match ` expression has a * head expression* , which is the value to compare to
@@ -99,11 +99,12 @@ symbols, as appropriate. For example, these two matches on `x: &i32` are
99
99
equivalent:
100
100
101
101
``` rust
102
- # let x = & 3 ;
103
- let y = match * x { 0 => " zero" , _ => " some" };
104
- let z = match x { & 0 => " zero" , _ => " some" };
102
+ let int_reference = & 3 ;
105
103
106
- assert_eq! (y , z );
104
+ let a = match * int_reference { 0 => " zero" , _ => " some" };
105
+ let b = match int_reference { & 0 => " zero" , _ => " some" };
106
+
107
+ assert_eq! (a , b );
107
108
```
108
109
109
110
Subpatterns can also be bound to variables by the use of the syntax `variable @
@@ -132,7 +133,7 @@ let message = match x {
132
133
assert_eq! (message , " a few" );
133
134
```
134
135
135
- Other forms of [ range] \( ` .. ` for an exclusive range, or any range with one or
136
+ Other forms of [ range] \( e.g ` .. ` for an exclusive range, or any range with one or
136
137
both endpoints left unspecified) are not supported in matches. The
137
138
syntax ` ... ` is also accepted for inclusive ranges in patterns only, for
138
139
backwards compatibility.
@@ -159,11 +160,16 @@ match v[..] {
159
160
}
160
161
```
161
162
162
- Finally, match patterns can accept * pattern guards* to further refine the
163
+ Finally, match arms can accept * pattern guards* to further refine the
163
164
criteria for matching a case. Pattern guards appear after the pattern and
164
165
consist of a bool-typed expression following the ` if ` keyword. A pattern guard
165
166
may refer to the variables bound within the pattern they follow.
166
167
168
+ When the pattern matches successfully, the pattern guard expression is executed.
169
+ If the expression is truthy, the pattern is successfully matched against.
170
+ Otherwise, the next pattern, including other matches with the ` | ` operator in
171
+ the same arm, is tested.
172
+
167
173
``` rust
168
174
# let maybe_digit = Some (0 );
169
175
# fn process_digit (i : i32 ) { }
@@ -175,6 +181,21 @@ let message = match maybe_digit {
175
181
};
176
182
```
177
183
184
+ > Note: Multiple matches using the ` | ` operator can cause the pattern guard and
185
+ > and side effects it has to execute multiple times. For example:
186
+ >
187
+ > ``` rust
188
+ > use std :: cell :: Cell ;
189
+ > fn main () {
190
+ > let i : Cell <i32 > = Cell :: new (0 );
191
+ > match 1 {
192
+ > 1 | _ if { i . set (i . get () + 1 ); false } => {}
193
+ > _ => {}
194
+ > }
195
+ > assert_eq! (i . get (), 2 );
196
+ > }
197
+ > ```
198
+
178
199
## Attributes on match arms
179
200
180
201
Outer attributes are allowed on match arms . The only attributes that have
@@ -190,3 +211,4 @@ meaning on match arms are [`cfg`], `cold`, and the [lint check attributes].
190
211
[_OuterAttribute_ ]: attributes . html
191
212
[`cfg `]: attributes . html#conditional - compilation
192
213
[lint check attributes ]: attributes . html#lint - check - attributes
214
+ [range ]: expressions / range - expr . html
0 commit comments