Skip to content

Commit fc172c1

Browse files
committed
Address review comments.
1 parent 939015a commit fc172c1

File tree

3 files changed

+101
-102
lines changed

3 files changed

+101
-102
lines changed

src/expressions/loop-expr.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ is equivalent to
109109

110110
> **<sup>Syntax</sup>**\
111111
> _IteratorLoopExpression_ :\
112-
> &nbsp;&nbsp; `for` _Pattern_ `in` [_Expression_]<sub>except struct expression</sub>
112+
> &nbsp;&nbsp; `for` [_Pattern_] `in` [_Expression_]<sub>except struct expression</sub>
113113
> [_BlockExpression_]
114114
115115
A `for` expression is a syntactic construct for looping over elements provided

src/expressions/match-expr.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,6 @@ Please notice that the `2..=9` is a [Range Pattern], not a [Range Expression]
8181
and, thus, only those types of ranges supported by range patterns can be used
8282
in match arms.
8383

84-
A range pattern may not be a sub-range of another range pattern inside the same `match`.
85-
8684
Match arms can accept _match guards_ to further refine the
8785
criteria for matching a case. Pattern guards appear after the pattern and
8886
consist of a bool-typed expression following the `if` keyword. A pattern guard

src/patterns.md

Lines changed: 100 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
> **<sup>Syntax</sup>**\
44
> _Pattern_ :\
55
> &nbsp;&nbsp; &nbsp;&nbsp; [_LiteralPattern_]\
6+
> &nbsp;&nbsp; | [_IdentifierPattern_]\
67
> &nbsp;&nbsp; | [_WildcardPattern_]\
78
> &nbsp;&nbsp; | [_RangePattern_]\
89
> &nbsp;&nbsp; | [_ReferencePattern_]\
9-
> &nbsp;&nbsp; | [_IdentifierPattern_]\
1010
> &nbsp;&nbsp; | [_StructPattern_]\
1111
> &nbsp;&nbsp; | [_TupleStructPattern_]\
1212
> &nbsp;&nbsp; | [_TuplePattern_]\
@@ -59,11 +59,12 @@ does four things:
5959
Patterns are used in:
6060

6161
* [`let` declarations](statements.html#let-statements)
62-
* [Function](items.html#functions) and [closure](expressions.html#closure-expressions)
62+
* [Function](items/functions.html) and [closure](expressions/closure-expr.html)
6363
parameters
64-
* [`match` expressions](expressions.html#match-expressions)
65-
* [`if let` expressions](expressions.html#if-let-expressions)
66-
* [`while let` expressions](expressions.html#while-let-loops)
64+
* [`match` expressions](expressions/match-expr.html)
65+
* [`if let` expressions](expressions/if-expr.html)
66+
* [`while let` expressions](expressions/loop-expr.html#predicate-pattern-loops)
67+
* [`for` expressions](expressions/loop-expr.html#iterator-loops)
6768
* Inside other patterns
6869

6970
## Destructuring
@@ -73,7 +74,7 @@ breaks a value up into its component pieces. The syntax used is almost the same
7374
when creating such values. When destructing a data structure with named (but
7475
not numbered) fields, it is allowed to write `fieldname` as a shorthand for
7576
`fieldname: fieldname`. In a pattern whose head expression has a `struct`,
76-
`enum` or `tupl` type, a placeholder (`_`) stands for a *single* data field,
77+
`enum` or `tuple` type, a placeholder (`_`) stands for a *single* data field,
7778
whereas a wildcard `..` stands for *all* the remaining fields of a particular variant.
7879

7980
```rust
@@ -158,6 +159,88 @@ for i in -2..5 {
158159
}
159160
```
160161

162+
## Identifier patterns
163+
164+
> **<sup>Syntax</sup>**\
165+
> _IdentifierPattern_ :\
166+
> &nbsp;&nbsp; &nbsp;&nbsp; `ref`<sup>?</sup> `mut`<sup>?</sup> IDENTIFIER (`@` [_Pattern_] ) <sup>?</sup>
167+
168+
_Identifier patterns_ bind the value they match to a **previously undeclared** variable.
169+
170+
Patterns that consist of only an identifier, possibly with a `mut`, like
171+
`variable`, `x`, and `y` below:
172+
173+
```rust
174+
let mut variable = 10;
175+
fn sum(x: i32, y: i32) -> i32 {
176+
# x + y
177+
# }
178+
```
179+
180+
match any value and bind it to that identifier. This is the most commonly
181+
used pattern in variable declarations and function/closure parameters.
182+
183+
To bind non-trivial patterns to a variable, the use of the syntax `variable @
184+
subpattern` is needed. For example:
185+
186+
```rust
187+
let x = 2;
188+
189+
match x {
190+
e @ 1 ..= 5 => println!("got a range element {}", e),
191+
_ => println!("anything"),
192+
}
193+
```
194+
195+
binds to `e` the value 2 (not the entire range: the range here is a range subpattern).
196+
197+
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]).
199+
This can be changed to bind to a reference by using the `ref` keyword,
200+
or to a mutable reference using `ref mut`. For example:
201+
202+
```rust
203+
# let a = Some(10);
204+
match a {
205+
None => (),
206+
Some(value) => (),
207+
}
208+
209+
match a {
210+
None => (),
211+
Some(ref value) => (),
212+
}
213+
```
214+
215+
in the first match expression, the value is copied (or moved). In the second match,
216+
a reference to the same memory location is bound to the variable value. This syntax is
217+
needed because in destructuring subpatterns we can't apply the `&` operator to
218+
the value's fields. For example:
219+
220+
```rust,compile_fail
221+
# struct Person {
222+
# name: String,
223+
# age: u8,
224+
# }
225+
# let value = Person{ name: String::from("John"), age: 23 };
226+
if let Person{name: &person_name, age: 18..=150} = value { }
227+
```
228+
229+
is not valid. What we must do is:
230+
231+
```rust
232+
# struct Person {
233+
# name: String,
234+
# age: u8,
235+
# }
236+
# let value = Person{ name: String::from("John"), age: 23 };
237+
if let Person{name: ref person_name, age: 18..=150} = value { }
238+
```
239+
240+
Thus, `ref` is not something that is being matched against. Its objective is
241+
exclusively to make the matched binding a reference, instead of potentially
242+
copying or moving what was matched.
243+
161244
## Wildcard pattern
162245

163246
> **<sup>Syntax</sup>**\
@@ -166,7 +249,8 @@ for i in -2..5 {
166249
167250
The _wildcard pattern_ matches any value. It is used to ignore values when they don't
168251
matter. Inside other patterns it matches a single data field (as opposed to the `..`
169-
which matches the remaining fields).
252+
which matches the remaining fields). Unlike identifier patterns, it does not copy, move
253+
or borrow the value it matches.
170254

171255
Examples:
172256

@@ -220,8 +304,8 @@ Range patterns match values that are within the closed range defined by its lowe
220304
upper bounds. For example, a pattern `'m'..='p'` will match only the values `'m'`, `'n'`,
221305
`'o'`, and `'p'`. The bounds can be literals or paths that point to constant values.
222306

223-
A pattern a `..=` b must always have a &le; b. Thus, it is not possible to have a range
224-
pattern `10..=0`, for example.
307+
A pattern a `..=` b must always have a &le; b. It is an error to have a range pattern
308+
`10..=0`, for example.
225309

226310
The `...` syntax is kept for backwards compatibility.
227311

@@ -300,7 +384,6 @@ println!("{}", match 0xfacade {
300384
0 ..= <u32 as MaxValue>::MAX => "fits in a u32",
301385
_ => "too big",
302386
});
303-
304387
```
305388

306389
Range patterns are always refutable, even when they cover the complete set
@@ -332,89 +415,6 @@ because it is a token by itself, not two `&` tokens.
332415

333416
Reference patterns are always irrefutable.
334417

335-
## Identifier patterns
336-
337-
> **<sup>Syntax</sup>**\
338-
> _IdentifierPattern_ :\
339-
> &nbsp;&nbsp; &nbsp;&nbsp; `mut`<sup>?</sup> IDENTIFIER (`@` [_Pattern_] ) <sup>?</sup>\
340-
> &nbsp;&nbsp; | `ref` `mut`<sup>?</sup> IDENTIFIER (`@` [_Pattern_] ) <sup>?</sup>
341-
342-
_Identifier patterns_ bind the value they match to a **previously undeclared** variable.
343-
344-
Patterns that consist of only an identifier, possibly with a `mut`, like
345-
`variable`, `x`, and `y` below:
346-
347-
```rust
348-
let mut variable = 10;
349-
fn sum(x: i32, y: i32) -> i32 {
350-
# x + y
351-
# }
352-
```
353-
354-
match any value and bind it to that identifier. This is the most commonly
355-
used pattern in variable declarations and function/closure parameters.
356-
357-
To bind non-trivial patterns to a variable, the use of the syntax `variable @
358-
subpattern` is needed. For example:
359-
360-
```rust
361-
let x = 2;
362-
363-
match x {
364-
e @ 1 ..= 5 => println!("got a range element {}", e),
365-
_ => println!("anything"),
366-
}
367-
```
368-
369-
binds to `e` the value 2 (not the entire range: the range here is a range subpattern).
370-
371-
By default, identifier patterns bind a variable to a copy of or move from the
372-
matched value (depending whether the matched value implements the [Copy trait]).
373-
This can be changed to bind to a reference by using the `ref` keyword,
374-
or to a mutable reference using `ref mut`. For example:
375-
376-
```rust
377-
# let a = Some(10);
378-
match a {
379-
None => (),
380-
Some(value) => (),
381-
}
382-
383-
match a {
384-
None => (),
385-
Some(ref value) => (),
386-
}
387-
```
388-
389-
in the first match expression, the value is copied (or moved). In the second match,
390-
a reference to the same memory location is bound to the variable value. This syntax is
391-
needed because in destructuring subpatterns we can't apply the `&` operator to
392-
the value's fields. For example:
393-
394-
```rust,compile_fail
395-
# struct Person {
396-
# name: String,
397-
# age: u8,
398-
# }
399-
# let value = Person{ name: String::from("John"), age: 23 };
400-
if let Person{& name: person_name, age: 18..=150} = value { }
401-
```
402-
403-
is not valid. What we must do is:
404-
405-
```rust
406-
# struct Person {
407-
# name: String,
408-
# age: u8,
409-
# }
410-
# let value = Person{ name: String::from("John"), age: 23 };
411-
if let Person{name: ref person_name, age: 18..=150} = value { }
412-
```
413-
414-
Thus, `ref` is not something that is being matched against. Its objective is
415-
exclusively to make the matched binding a reference, instead of potentially
416-
copying or moving what was matched.
417-
418418
## Struct patterns
419419

420420
> **<sup>Syntax</sup>**\
@@ -443,9 +443,9 @@ copying or moving what was matched.
443443
> &nbsp;&nbsp; `..`
444444
445445
Struct patterns match struct values that match all criteria defined by its subpatterns.
446-
They are also used to [destructure](destructuring) a struct.
446+
They are also used to [destructure](#destructuring) a struct.
447447

448-
On a struct pattern, the fields are referenced by name, index (in the case of tuples
448+
On a struct pattern, the fields are referenced by name, index (in the case of tuple
449449
structs) or ignored by use of `..`:
450450

451451
```rust
@@ -522,7 +522,7 @@ A struct pattern is refutable when one of its subpatterns is refutable.
522522
> &nbsp;&nbsp; | ([_Pattern_] `,`)<sup>\*</sup> `..` ( (`,` [_Pattern_])<sup>+</sup> `,`<sup>?</sup> )<sup>?</sup>
523523
524524
TupleStruct patterns match tuple struct and enum values that match all criteria defined
525-
by its subpatterns. They are also used to [destructure](destructuring) a tuple struct or
525+
by its subpatterns. They are also used to [destructure](#destructuring) a tuple struct or
526526
enum value.
527527

528528
A TupleStruct pattern is refutable when one of its subpatterns is refutable.
@@ -539,7 +539,7 @@ A TupleStruct pattern is refutable when one of its subpatterns is refutable.
539539
> &nbsp;&nbsp; | ([_Pattern_] `,`)<sup>\*</sup> `..` ( (`,` [_Pattern_])<sup>+</sup> `,`<sup>?</sup> )<sup>?</sup>
540540
541541
Tuple patterns match tuple values that match all criteria defined by its subpatterns.
542-
They are also used to [destructure](destructuring) a tuple.
542+
They are also used to [destructure](#destructuring) a tuple.
543543

544544
This pattern is refutable when one of its subpatterns is refutable.
545545

@@ -587,8 +587,9 @@ Unqualified path patterns can refer to:
587587

588588
Qualified path patterns can only refer to associated constants.
589589

590-
Path patterns are irrefutable when they refer to constants or structs.
591-
They are refutable when the refer to enum variants.
590+
Path patterns are irrefutable when they refer to structs or an enum variant when the enum
591+
has only one variant or a constant whose type is irrefutable. They are refutable when they
592+
refer to refutable constants or enum variants for enums with multiple variants.
592593

593594
[_Pattern_]: #patterns
594595
[_LiteralPattern_]: #literal-patterns

0 commit comments

Comments
 (0)