@@ -12,7 +12,7 @@ Various changes to the match ergonomics rules:
12
12
references.
13
13
- On edition ≥ 2024, ` mut ` on an identifier pattern does not force its binding
14
14
mode to by-value.
15
- - On edition ≥ 2024 , ` & ` patterns can match against ` &mut ` references.
15
+ - On all editions , ` & ` patterns can match against ` &mut ` references.
16
16
- On all editions, the binding mode can no longer ever be implicitly set to
17
17
` ref mut ` behind an ` & ` pattern.
18
18
@@ -34,11 +34,11 @@ let (x, mut y) = &(true, false);
34
34
let _ : (& bool , bool ) = (x , y );
35
35
```
36
36
37
- ## Can' t cancel out an inherited reference
37
+ ## Can’ t cancel out an inherited reference
38
38
39
39
` & ` and ` &mut ` patterns must correspond with a reference in the same position in
40
40
the scrutinee, even if there is an inherited reference present. Therefore, users
41
- have no general mechanism to " cancel out" an inherited reference
41
+ have no general mechanism to “ cancel out” an inherited reference
42
42
(< https://users.rust-lang.org/t/reference-of-tuple-and-tuple-of-reference/91713/6 > ,
43
43
< https://users.rust-lang.org/t/cannot-deconstruct-reference-inside-match-on-reference-why/92147 > ,
44
44
< https://github.com/rust-lang/rust/issues/50008 > ,
@@ -71,8 +71,8 @@ Match ergonomics works a little differently in edition 2024 and above.
71
71
72
72
## ` mut ` no longer strips the inherited reference
73
73
74
- ` mut ` on a binding does not reset the binding mode on edition ≥ 2024.
75
- Instead, ` mut ` on a binding with non-default binding mode is an error.
74
+ ` mut ` on a binding does not reset the binding mode on edition ≥ 2024. Instead,
75
+ ` mut ` on a binding with non-default binding mode is an error.
76
76
77
77
``` rust
78
78
// ! Edition ≥ 2024
@@ -115,15 +115,21 @@ let _: &u8 = x;
115
115
116
116
## ` & ` matches against ` &mut `
117
117
118
- In edition 2024 and above , ` & ` patterns can match against ` &mut ` references
119
- (including "inherited" references).
118
+ On all editions , ` & ` patterns can match against ` &mut ` references (on edition
119
+ 2024 and above, this includes "inherited" references).
120
120
121
121
``` rust
122
- // ! Edition ≥ 2024
122
+ // ! All editions
123
123
let & foo = & mut 42 ;
124
124
let _ : u8 = foo ;
125
125
```
126
126
127
+ ``` rust
128
+ // ! Edition ≥ 2024
129
+ let [& foo ] = & mut [42 ];
130
+ let _ : u8 = foo ;
131
+ ```
132
+
127
133
# Reference-level explanation
128
134
[ reference-level-explanation ] : #reference-level-explanation
129
135
@@ -140,12 +146,12 @@ instead, `mut` on a binding with a by-reference binding mode is an error.
140
146
// let [mut a] = &[42]; //ERROR
141
147
```
142
148
143
- ## Edition 2024 : ` & ` patterns can match against ` &mut ` references
149
+ ## All editions : ` & ` patterns can match against ` &mut ` references
144
150
145
151
` & ` patterns can match against ` &mut ` references.
146
152
147
153
``` rust
148
- // ! Edition ≥ 2024
154
+ // ! All editions
149
155
let & foo = & mut 42 ;
150
156
let _ : u8 = foo ;
151
157
```
@@ -328,7 +334,7 @@ anyway is that the current behavior is unintuitive and surprising for users.
328
334
329
335
## Never setting default binding mode to ` ref mut ` behind ` & `
330
336
331
- ### We can' t delay this choice
337
+ ### We can’ t delay this choice
332
338
333
339
#### Patterns that work only with this rule
334
340
@@ -423,14 +429,14 @@ There are several motivations for allowing this:
423
429
- It makes refactoring less painful. Sometimes, one is not certain whether an
424
430
unfinished API will end up returning a shared or a mutable reference. But as
425
431
long as the reference returned by said API is not actually used to perform
426
- mutation, it often doesn' t matter either way, as ` &mut ` implicitly reborrows
432
+ mutation, it often doesn’ t matter either way, as ` &mut ` implicitly reborrows
427
433
as ` & ` in many situations. Pattern matching is currently one of the most
428
434
prominent exceptions to this, and match ergonomics magnifies the pain because
429
435
a reference in one part of the pattern can affect the binding mode in a
430
436
different, faraway location[ ^ nrmba ] . If patterns can be written to always use
431
437
` & ` unless mutation is required, then the amount of editing necessary to
432
438
perform various refactors is lessened.
433
- - It' s intuitive. ` &mut ` is strictly more powerful than ` & ` . It' s conceptually a
439
+ - It’ s intuitive. ` &mut ` is strictly more powerful than ` & ` . It’ s conceptually a
434
440
subtype, and even if not implemented that way[ ^ sub ] , coercions mean it often
435
441
feels like one in practice.
436
442
@@ -478,7 +484,7 @@ concerns with certain proposals for "deref patterns".
478
484
479
485
## Deref patterns
480
486
481
- Because it is compositional, the " eat-one-layer" model proposed by this RFC is
487
+ Because it is compositional, the “ eat-one-layer” model proposed by this RFC is
482
488
fully compatible with proposals for "deref patterns", including allowing
483
489
` & ` /` &mut ` patterns to match against types implementing ` Deref ` /` DerefMut ` . One
484
490
question that would need to be resolved is whether and how deref patterns
0 commit comments