@@ -17,7 +17,14 @@ Patterns are used to match values against structures and to,
17
17
optionally, bind variables to values inside these structures. They are also
18
18
used in variable declarations and parameters for functions and closures.
19
19
20
- For example, the pattern used in:
20
+ The pattern in the following example does four things:
21
+
22
+ * Tests if ` person ` has the ` car ` field filled with something.
23
+ * Tests if the person's ` age ` field is between 13 and 19, and binds its value to
24
+ the ` person_age ` variable.
25
+ * Binds a reference to the ` name ` field to the variable ` person_name ` .
26
+ * Ignores the rest of the fields of ` person ` . The remaining fields can have any value and
27
+ are not bound to any variables.
21
28
22
29
``` rust
23
30
# struct Car ;
@@ -45,14 +52,6 @@ if let
45
52
println! (" {} has a car and is {} years old." , person_name , person_age );
46
53
}
47
54
```
48
- does four things:
49
-
50
- * Tests if ` person ` has the ` car ` field filled with something.
51
- * Tests if the person's ` age ` field is between 13 and 19, and binds its value to
52
- the ` person_age ` variable.
53
- * Binds a reference to the ` name ` field to the variable ` person_name ` .
54
- * Ignores the rest of the fields of ` person ` , i.e., they can have any value and
55
- are not bound to any variables.
56
55
57
56
Patterns are used in:
58
57
@@ -173,8 +172,9 @@ must be unique within the pattern. The variable will shadow any variables of
173
172
the same name in scope. The scope of the new binding depends on the context of
174
173
where the pattern is used (such as a ` let ` binding or a ` match ` arm).
175
174
176
- Patterns that consist of only an identifier, possibly with a ` mut ` , like
177
- ` variable ` , ` x ` , and ` y ` below:
175
+ Patterns that consist of only an identifier, possibly with a ` mut ` , match any value and
176
+ bind it to that identifier. This is the most commonly used pattern in variable
177
+ declarations and parameters for functions and closures.
178
178
179
179
``` rust
180
180
let mut variable = 10 ;
@@ -183,11 +183,9 @@ fn sum(x: i32, y: i32) -> i32 {
183
183
# }
184
184
```
185
185
186
- match any value and bind it to that identifier. This is the most commonly
187
- used pattern in variable declarations and parameters for functions and closures.
188
-
189
- To bind non-trivial patterns to a variable, the use of the syntax `variable @
190
- subpattern` is needed. For example:
186
+ To bind the matched value of a pattern to a variable, use the syntax `variable @
187
+ subpattern` . For example, the following binds the value 2 to ` e` (not the
188
+ entire range: the range here is a range subpattern).
191
189
192
190
``` rust
193
191
let x = 2 ;
@@ -198,8 +196,6 @@ match x {
198
196
}
199
197
```
200
198
201
- binds to ` e ` the value 2 (not the entire range: the range here is a range subpattern).
202
-
203
199
By default, identifier patterns bind a variable to a copy of or move from the
204
200
matched value depending on whether the matched value implements [ ` Copy ` ] .
205
201
This can be changed to bind to a reference by using the ` ref ` keyword,
@@ -218,10 +214,10 @@ match a {
218
214
}
219
215
```
220
216
221
- in the first match expression, the value is copied (or moved). In the second match,
217
+ In the first match expression, the value is copied (or moved). In the second match,
222
218
a reference to the same memory location is bound to the variable value. This syntax is
223
- needed because in destructuring subpatterns we can't apply the ` & ` operator to
224
- the value's fields. For example:
219
+ needed because in destructuring subpatterns the ` & ` operator can't be applied to
220
+ the value's fields. For example, the following is not valid :
225
221
226
222
``` rust,compile_fail
227
223
# struct Person {
@@ -232,7 +228,7 @@ the value's fields. For example:
232
228
if let Person{name: &person_name, age: 18..=150} = value { }
233
229
```
234
230
235
- is not valid. What we must do is :
231
+ To make it valid, write the following :
236
232
237
233
``` rust
238
234
# struct Person {
@@ -252,7 +248,7 @@ if `ref` or `ref mut` is specified and the identifier shadows a constant.
252
248
253
249
### Binding modes
254
250
255
- In order to service better ergonomics, patterns operate in different * binding modes* in
251
+ To service better ergonomics, patterns operate in different * binding modes* in
256
252
order to make it easier to bind references to values. When a reference value is matched by
257
253
a non-reference pattern, it will be automatically treated as a ` ref ` or ` ref mut ` binding.
258
254
Example:
@@ -269,7 +265,7 @@ patterns](#wildcard-pattern) (`_`), [`const` patterns](#path-patterns) of refere
269
265
and [ reference patterns] ( #reference-patterns ) .
270
266
271
267
If a binding pattern does not explicitly have ` ref ` , ` ref mut ` , or ` mut ` , then it uses the
272
- * default binding mode* to determine how the variable should be bound. The default binding
268
+ * default binding mode* to determine how the variable is bound. The default binding
273
269
mode starts in "move" mode which uses move semantics. When matching a pattern, the
274
270
compiler starts from the outside of the pattern and works inwards. Each time a reference
275
271
is matched using a non-reference pattern, it will automatically dereference the value and
@@ -553,7 +549,7 @@ let Struct{a: x, b: y, c: z} = struct_value; // destructure all fields
553
549
554
550
A struct pattern is refutable when one of its subpatterns is refutable.
555
551
556
- ## TupleStruct patterns
552
+ ## Tuple struct patterns
557
553
558
554
> ** <sup >Syntax</sup >** \
559
555
> _ TupleStructPattern_ :\
@@ -563,16 +559,16 @@ A struct pattern is refutable when one of its subpatterns is refutable.
563
559
>   ;  ;   ;  ; [ _ Pattern_ ]   ; ( ` , ` [ _ Pattern_ ] )<sup >\* </sup > ` , ` <sup >?</sup >\
564
560
>   ;  ; | ([ _ Pattern_ ] ` , ` )<sup >\* </sup > ` .. ` ( (` , ` [ _ Pattern_ ] )<sup >+</sup > ` , ` <sup >?</sup > )<sup >?</sup >
565
561
566
- TupleStruct patterns match tuple struct and enum values that match all criteria defined
562
+ Tuple struct patterns match tuple struct and enum values that match all criteria defined
567
563
by its subpatterns. They are also used to [ destructure] ( #destructuring ) a tuple struct or
568
564
enum value.
569
565
570
- A TupleStruct pattern is refutable when one of its subpatterns is refutable.
566
+ A tuple struct pattern is refutable when one of its subpatterns is refutable.
571
567
572
568
## Tuple patterns
573
569
574
570
> ** <sup >Syntax</sup >** \
575
- > _ TuplePattern_ :< a name = " tuple-pattern-syntax " ></ a > \
571
+ > _ TuplePattern_ :\
576
572
>   ;  ; ` ( ` _ TuplePatternItems_ <sup >?</sup > ` ) `
577
573
>
578
574
> _ TuplePatternItems_ :\
@@ -606,7 +602,7 @@ let v = vec![1, 2, 3];
606
602
match v [.. ] {
607
603
[a , b ] => { /* this arm will not apply because the length doesn't match */ }
608
604
[a , b , c ] => { /* this arm will apply */ }
609
- _ => { /* this wildcard is required, since we don't know length statically */ }
605
+ _ => { /* this wildcard is required, since the length is not known statically */ }
610
606
};
611
607
```
612
608
@@ -642,7 +638,7 @@ refer to refutable constants or enum variants for enums with multiple variants.
642
638
[ _RangePattern_ ] : #range-patterns
643
639
[ _ReferencePattern_ ] : #reference-patterns
644
640
[ _IdentifierPattern_ ] : #identifier-patterns
645
- [ _TupleStructPattern_ ] : #tuplestruct -patterns
641
+ [ _TupleStructPattern_ ] : #tuple-struct -patterns
646
642
[ _StructPattern_ ] : #struct-patterns
647
643
[ _TuplePattern_ ] : #tuple-patterns
648
644
[ _SlicePattern_ ] : #slice-patterns
0 commit comments