Skip to content

Commit dd1e2e6

Browse files
committed
More review comments.
1 parent 4ea8e34 commit dd1e2e6

File tree

1 file changed

+26
-30
lines changed

1 file changed

+26
-30
lines changed

src/patterns.md

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,14 @@ Patterns are used to match values against structures and to,
1717
optionally, bind variables to values inside these structures. They are also
1818
used in variable declarations and parameters for functions and closures.
1919

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.
2128

2229
```rust
2330
# struct Car;
@@ -45,14 +52,6 @@ if let
4552
println!("{} has a car and is {} years old.", person_name, person_age);
4653
}
4754
```
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.
5655

5756
Patterns are used in:
5857

@@ -173,8 +172,9 @@ must be unique within the pattern. The variable will shadow any variables of
173172
the same name in scope. The scope of the new binding depends on the context of
174173
where the pattern is used (such as a `let` binding or a `match` arm).
175174

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.
178178

179179
```rust
180180
let mut variable = 10;
@@ -183,11 +183,9 @@ fn sum(x: i32, y: i32) -> i32 {
183183
# }
184184
```
185185

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).
191189

192190
```rust
193191
let x = 2;
@@ -198,8 +196,6 @@ match x {
198196
}
199197
```
200198

201-
binds to `e` the value 2 (not the entire range: the range here is a range subpattern).
202-
203199
By default, identifier patterns bind a variable to a copy of or move from the
204200
matched value depending on whether the matched value implements [`Copy`].
205201
This can be changed to bind to a reference by using the `ref` keyword,
@@ -218,10 +214,10 @@ match a {
218214
}
219215
```
220216

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,
222218
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:
225221

226222
```rust,compile_fail
227223
# struct Person {
@@ -232,7 +228,7 @@ the value's fields. For example:
232228
if let Person{name: &person_name, age: 18..=150} = value { }
233229
```
234230

235-
is not valid. What we must do is:
231+
To make it valid, write the following:
236232

237233
```rust
238234
# struct Person {
@@ -252,7 +248,7 @@ if `ref` or `ref mut` is specified and the identifier shadows a constant.
252248

253249
### Binding modes
254250

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
256252
order to make it easier to bind references to values. When a reference value is matched by
257253
a non-reference pattern, it will be automatically treated as a `ref` or `ref mut` binding.
258254
Example:
@@ -269,7 +265,7 @@ patterns](#wildcard-pattern) (`_`), [`const` patterns](#path-patterns) of refere
269265
and [reference patterns](#reference-patterns).
270266

271267
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
273269
mode starts in "move" mode which uses move semantics. When matching a pattern, the
274270
compiler starts from the outside of the pattern and works inwards. Each time a reference
275271
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
553549

554550
A struct pattern is refutable when one of its subpatterns is refutable.
555551

556-
## TupleStruct patterns
552+
## Tuple struct patterns
557553

558554
> **<sup>Syntax</sup>**\
559555
> _TupleStructPattern_ :\
@@ -563,16 +559,16 @@ A struct pattern is refutable when one of its subpatterns is refutable.
563559
> &nbsp;&nbsp; &nbsp;&nbsp; [_Pattern_]&nbsp;( `,` [_Pattern_] )<sup>\*</sup> `,`<sup>?</sup>\
564560
> &nbsp;&nbsp; | ([_Pattern_] `,`)<sup>\*</sup> `..` ( (`,` [_Pattern_])<sup>+</sup> `,`<sup>?</sup> )<sup>?</sup>
565561
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
567563
by its subpatterns. They are also used to [destructure](#destructuring) a tuple struct or
568564
enum value.
569565

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.
571567

572568
## Tuple patterns
573569

574570
> **<sup>Syntax</sup>**\
575-
> _TuplePattern_ :<a name="tuple-pattern-syntax"></a>\
571+
> _TuplePattern_ :\
576572
> &nbsp;&nbsp; `(` _TuplePatternItems_<sup>?</sup> `)`
577573
>
578574
> _TuplePatternItems_ :\
@@ -606,7 +602,7 @@ let v = vec![1, 2, 3];
606602
match v[..] {
607603
[a, b] => { /* this arm will not apply because the length doesn't match */ }
608604
[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 */ }
610606
};
611607
```
612608

@@ -642,7 +638,7 @@ refer to refutable constants or enum variants for enums with multiple variants.
642638
[_RangePattern_]: #range-patterns
643639
[_ReferencePattern_]: #reference-patterns
644640
[_IdentifierPattern_]: #identifier-patterns
645-
[_TupleStructPattern_]: #tuplestruct-patterns
641+
[_TupleStructPattern_]: #tuple-struct-patterns
646642
[_StructPattern_]: #struct-patterns
647643
[_TuplePattern_]: #tuple-patterns
648644
[_SlicePattern_]: #slice-patterns

0 commit comments

Comments
 (0)