Skip to content

Commit 52a335e

Browse files
committed
Address patterns review comments.
1 parent 6216f45 commit 52a335e

File tree

3 files changed

+63
-54
lines changed

3 files changed

+63
-54
lines changed

src/expressions/match-expr.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
>    `if` [_Expression_]
2525
2626
A *`match` expression* branches on a pattern. The exact form of matching that
27-
occurs depends on the pattern. See [Patterns] for more details. A `match`
27+
occurs depends on the [pattern][Patterns]. A `match`
2828
expression has a *head expression*, which is the value to compare to the
29-
patterns. The type of the patterns must equal the type of the head expression.
29+
patterns. The head expression and the patterns must have the same type.
3030

3131
A `match` behaves differently depending on whether or not the head expression
3232
is a [place expression or value expression][place expression].
@@ -59,10 +59,8 @@ match x {
5959
}
6060
```
6161

62-
Patterns that bind variables default to binding to a copy or move of the
63-
matched value (depending on the matched value's type). This can be changed to
64-
bind to a reference by using the `ref` keyword, or to a mutable reference using
65-
`ref mut`. See [Identifier Patterns].
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.
6664

6765
Multiple match patterns may be joined with the `|` operator:
6866

@@ -139,3 +137,4 @@ meaning on match arms are [`cfg`], `cold`, and the [lint check attributes].
139137
[Tuple Struct Patterns]: patterns.html#tuplestruct-patterns
140138
[Tuple Patterns]: patterns.html#tuple-patterns
141139
[Range Pattern]: patterns.html#range-patterns
140+
[binding mode]: patterns.html#binding-modes

src/patterns.md

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@
1313
>    | [_SlicePattern_]\
1414
>    | [_PathPattern_]
1515
16-
Patterns in Rust are used to match values against structures and to,
16+
Patterns are used to match values against structures and to,
1717
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.
2119

2220
For example, the pattern used in:
2321

@@ -65,17 +63,17 @@ Patterns are used in:
6563
* [`if let` expressions](expressions/if-expr.html)
6664
* [`while let` expressions](expressions/loop-expr.html#predicate-pattern-loops)
6765
* [`for` expressions](expressions/loop-expr.html#iterator-loops)
68-
* Inside other patterns
6966

7067
## Destructuring
7168

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`.
7977

8078
```rust
8179
# enum Message {
@@ -98,7 +96,7 @@ match message {
9896

9997
## Refutability
10098

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
102100
by the value it is being matched against. *Irrefutable* patterns, on the other hand,
103101
always match the value they are being matched against. Examples:
104102

@@ -136,14 +134,19 @@ if let (a, 3) = (1, 2) { // "(a, 3)" is refutable, and will not match
136134
[INTEGER_LITERAL]: tokens.html#integer-literals
137135
[FLOAT_LITERAL]: tokens.html#floating-point-literals
138136

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">
142143

143144
Floating-point literals are currently accepted, but due to the complexity of comparing
144145
them, they are going to be forbidden on literal patterns in a future version of Rust (see
145146
[issue #41620](https://github.com/rust-lang/rust/issues/41620)).
146147

148+
</div>
149+
147150
Literal patterns are always refutable.
148151

149152
Examples:
@@ -165,7 +168,10 @@ for i in -2..5 {
165168
> _IdentifierPattern_ :\
166169
> &nbsp;&nbsp; &nbsp;&nbsp; `ref`<sup>?</sup> `mut`<sup>?</sup> [IDENTIFIER] (`@` [_Pattern_] ) <sup>?</sup>
167170
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).
169175

170176
Patterns that consist of only an identifier, possibly with a `mut`, like
171177
`variable`, `x`, and `y` below:
@@ -178,7 +184,7 @@ fn sum(x: i32, y: i32) -> i32 {
178184
```
179185

180186
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.
182188

183189
To bind non-trivial patterns to a variable, the use of the syntax `variable @
184190
subpattern` is needed. For example:
@@ -195,7 +201,7 @@ match x {
195201
binds to `e` the value 2 (not the entire range: the range here is a range subpattern).
196202

197203
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].
199205
This can be changed to bind to a reference by using the `ref` keyword,
200206
or to a mutable reference using `ref mut`. For example:
201207

@@ -642,5 +648,9 @@ refer to refutable constants or enum variants for enums with multiple variants.
642648
[_SlicePattern_]: #slice-patterns
643649
[_PathPattern_]: #path-patterns
644650

645-
[Copy trait]: special-types-and-traits.html#copy
651+
[Copy]: special-types-and-traits.html#copy
646652
[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

src/tokens.md

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -577,42 +577,42 @@ them are referred to as "token trees" in [macros]. The three types of brackets
577577

578578

579579
[Operator expressions]: expressions/operator-expr.html
580-
[tokens]: #tokens
581-
[keywords]: keywords.html
582-
[identifier]: identifiers.html
583-
[tuples]: types.html#tuple-types
584-
[tuple structs]: items/structs.html
585-
[tuple variants]: items/enumerations.html
580+
[Range patterns]: patterns.html#range-patterns
581+
[Reference patterns]: patterns.html#reference-patterns
582+
[Subpattern binding]: patterns.html#identifier-patterns
583+
[Wildcard patterns]: patterns.html#wildcard-pattern
586584
[arith]: expressions/operator-expr.html#arithmetic-and-logical-binary-operators
587-
[negation]: expressions/operator-expr.html#negation-operators
588-
[lazy-bool]: expressions/operator-expr.html#lazy-boolean-operators
589-
[compound]: expressions/operator-expr.html#compound-assignment-expressions
590-
[comparison]: expressions/operator-expr.html#comparison-operators
591-
[match]: expressions/match-expr.html
592-
[field]: expressions/field-expr.html
593-
[range]: expressions/range-expr.html
594-
[trait bounds]: trait-bounds.html
595-
[dereference]: expressions/operator-expr.html#the-dereference-operator
596-
[raw pointers]: types.html#raw-pointers-const-and-mut
597-
[macros]: macros-by-example.html
585+
[array types]: types.html#array-and-slice-types
586+
[assignment]: expressions/operator-expr.html#assignment-expressions
598587
[attributes]: attributes.html
599-
[never type]: types.html#never-type
600588
[borrow]: expressions/operator-expr.html#borrow-operators
601-
[references]: types.html#pointer-types
602589
[closures]: expressions/closure-expr.html
603-
[assignment]: expressions/operator-expr.html#assignment-expressions
590+
[comparison]: expressions/operator-expr.html#comparison-operators
591+
[compound]: expressions/operator-expr.html#compound-assignment-expressions
604592
[constant items]: items/constant-items.html
593+
[dereference]: expressions/operator-expr.html#the-dereference-operator
594+
[extern]: items/external-blocks.html
595+
[field]: expressions/field-expr.html
596+
[functions]: items/functions.html
605597
[generics]: items/generics.html
598+
[identifier]: identifiers.html
599+
[keywords]: keywords.html
600+
[lazy-bool]: expressions/operator-expr.html#lazy-boolean-operators
601+
[macros]: macros-by-example.html
602+
[match]: expressions/match-expr.html
603+
[negation]: expressions/operator-expr.html#negation-operators
604+
[never type]: types.html#never-type
606605
[paths]: paths.html
607-
[array types]: types.html#array-and-slice-types
608-
[functions]: items/functions.html
606+
[patterns]: patterns.html
609607
[question]: expressions/operator-expr.html#the-question-mark-operator
608+
[range]: expressions/range-expr.html
609+
[raw pointers]: types.html#raw-pointers-const-and-mut
610+
[references]: types.html#pointer-types
610611
[sized]: trait-bounds.html#sized
611-
[extern]: items/external-blocks.html
612612
[struct expressions]: expressions/struct-expr.html
613+
[tokens]: #tokens
614+
[trait bounds]: trait-bounds.html
613615
[tuple index]: expressions/tuple-expr.html#tuple-indexing-expressions
614-
[Wildcard patterns]: patterns.html#wildcard-pattern
615-
[patterns]: patterns.html
616-
[Subpattern binding]: patterns.html#identifier-patterns
617-
[Range patterns]: patterns.html#range-patterns
618-
[Reference patterns]: patterns.html#reference-patterns
616+
[tuple structs]: items/structs.html
617+
[tuple variants]: items/enumerations.html
618+
[tuples]: types.html#tuple-types

0 commit comments

Comments
 (0)