13
13
>   ;  ; | [ _ SlicePattern_ ] \
14
14
>   ;  ; | [ _ PathPattern_ ]
15
15
16
- Patterns in Rust are used to match values against structures and to,
16
+ Patterns are used to match values against structures and to,
17
17
optionally, bind variables to values inside these structures. They are also
18
- used in variable declarations and function/closure parameters, though in these
19
- cases most of the time they are simply used as an identifier that binds to a
20
- value.
18
+ used in variable declarations and parameters for functions and closures.
21
19
22
20
For example, the pattern used in:
23
21
@@ -65,17 +63,17 @@ Patterns are used in:
65
63
* [ ` if let ` expressions] ( expressions/if-expr.html )
66
64
* [ ` while let ` expressions] ( expressions/loop-expr.html#predicate-pattern-loops )
67
65
* [ ` for ` expressions] ( expressions/loop-expr.html#iterator-loops )
68
- * Inside other patterns
69
66
70
67
## Destructuring
71
68
72
- Patterns can be used to * destructure* structs, enums, and tuples. Destructuring
73
- breaks a value up into its component pieces. The syntax used is almost the same as
74
- when creating such values. When destructing a data structure with named (but
75
- not numbered) fields, it is allowed to write ` fieldname ` as a shorthand for
76
- ` fieldname: fieldname ` . In a pattern whose head expression has a ` struct ` ,
77
- ` enum ` or ` tuple ` type, a placeholder (` _ ` ) stands for a * single* data field,
78
- whereas a wildcard ` .. ` stands for * all* the remaining fields of a particular variant.
69
+ Patterns can be used to * destructure* [ structs] , [ enums] , and [ tuples] .
70
+ Destructuring breaks up a value into its component pieces. The syntax used is
71
+ almost the same as when creating such values. In a pattern whose head
72
+ expression has a ` struct ` , ` enum ` or ` tuple ` type, a placeholder (` _ ` ) stands
73
+ in for a * single* data field, whereas a wildcard ` .. ` stands in for * all* the
74
+ remaining fields of a particular variant. When destructuring a data structure
75
+ with named (but not numbered) fields, it is allowed to write ` fieldname ` as a
76
+ shorthand for ` fieldname: fieldname ` .
79
77
80
78
``` rust
81
79
# enum Message {
@@ -98,7 +96,7 @@ match message {
98
96
99
97
## Refutability
100
98
101
- A pattern is said to be * Refutable * when it ** has the possibility of not being matched**
99
+ A pattern is said to be * refutable * when it has the possibility of not being matched
102
100
by the value it is being matched against. * Irrefutable* patterns, on the other hand,
103
101
always match the value they are being matched against. Examples:
104
102
@@ -136,14 +134,19 @@ if let (a, 3) = (1, 2) { // "(a, 3)" is refutable, and will not match
136
134
[ INTEGER_LITERAL ] : tokens.html#integer-literals
137
135
[ FLOAT_LITERAL ] : tokens.html#floating-point-literals
138
136
139
- _ Literal patterns_ match exactly the value they represent. Since negative numbers are
140
- not literals in Rust, literal patterns also accept an optional minus sign before the
141
- literal.
137
+ _ Literal patterns_ match exactly the same value as what is created by the
138
+ literal. Since negative numbers are not [ literals] , literal patterns also
139
+ accept an optional minus sign before the literal, which acts like the negation
140
+ operator.
141
+
142
+ <div class =" warning " >
142
143
143
144
Floating-point literals are currently accepted, but due to the complexity of comparing
144
145
them, they are going to be forbidden on literal patterns in a future version of Rust (see
145
146
[ issue #41620 ] ( https://github.com/rust-lang/rust/issues/41620 ) ).
146
147
148
+ </div >
149
+
147
150
Literal patterns are always refutable.
148
151
149
152
Examples:
@@ -165,7 +168,10 @@ for i in -2..5 {
165
168
> _ IdentifierPattern_ :\
166
169
>   ;  ;   ;  ; ` ref ` <sup >?</sup > ` mut ` <sup >?</sup > [ IDENTIFIER] (` @ ` [ _ Pattern_ ] ) <sup >?</sup >
167
170
168
- _ Identifier patterns_ bind the value they match to a ** previously undeclared** variable.
171
+ Identifier patterns bind the value they match to a variable. The identifier
172
+ must be unique within the pattern. The variable will shadow any variables of
173
+ the same name in scope. The scope of the new binding depends on the context of
174
+ where the pattern is used (such as a ` let ` binding versus a ` match ` arm).
169
175
170
176
Patterns that consist of only an identifier, possibly with a ` mut ` , like
171
177
` variable ` , ` x ` , and ` y ` below:
@@ -178,7 +184,7 @@ fn sum(x: i32, y: i32) -> i32 {
178
184
```
179
185
180
186
match any value and bind it to that identifier. This is the most commonly
181
- used pattern in variable declarations and function/closure parameters.
187
+ used pattern in variable declarations and parameters for functions and closures .
182
188
183
189
To bind non-trivial patterns to a variable, the use of the syntax `variable @
184
190
subpattern` is needed. For example:
@@ -195,7 +201,7 @@ match x {
195
201
binds to ` e ` the value 2 (not the entire range: the range here is a range subpattern).
196
202
197
203
By default, identifier patterns bind a variable to a copy of or move from the
198
- matched value ( depending whether the matched value implements the [ Copy trait ] ) .
204
+ matched value depending on whether the matched value implements [ Copy] .
199
205
This can be changed to bind to a reference by using the ` ref ` keyword,
200
206
or to a mutable reference using ` ref mut ` . For example:
201
207
@@ -642,5 +648,9 @@ refer to refutable constants or enum variants for enums with multiple variants.
642
648
[ _SlicePattern_ ] : #slice-patterns
643
649
[ _PathPattern_ ] : #path-patterns
644
650
645
- [ Copy trait ] : special-types-and-traits.html#copy
651
+ [ Copy ] : special-types-and-traits.html#copy
646
652
[ IDENTIFIER ] : identifiers.html
653
+ [ enums ] : items/enumerations.html
654
+ [ literals ] : expressions/literal-expr.html
655
+ [ structs ] : items/structs.html
656
+ [ tuples ] : types.html#tuple-types
0 commit comments