@@ -79,6 +79,23 @@ Match ergonomics works a little differently in edition 2024 and above.
79
79
// let (x, mut y) = &(true, false); // ERROR
80
80
```
81
81
82
+ ## ` & ` matches against ` &mut `
83
+
84
+ On all editions, ` & ` patterns can match against ` &mut ` references. On edition
85
+ 2024 and above, this includes "inherited" references as described below.
86
+
87
+ ``` rust
88
+ // ! All editions
89
+ let & foo = & mut 42 ;
90
+ let _ : u8 = foo ;
91
+ ```
92
+
93
+ ``` rust
94
+ // ! Edition ≥ 2024
95
+ let [& foo ] = & mut [42 ];
96
+ let _ : u8 = foo ;
97
+ ```
98
+
82
99
## Matching against inherited references
83
100
84
101
In all editions, when you match against an ` & ` or ` &mut ` reference with the type
@@ -113,23 +130,6 @@ let [&x] = &[&42];
113
130
let _ : & u8 = x ;
114
131
```
115
132
116
- ## ` & ` matches against ` &mut `
117
-
118
- On all editions, ` & ` patterns can match against ` &mut ` references (on edition
119
- 2024 and above, this includes "inherited" references).
120
-
121
- ``` rust
122
- // ! All editions
123
- let & foo = & mut 42 ;
124
- let _ : u8 = foo ;
125
- ```
126
-
127
- ``` rust
128
- // ! Edition ≥ 2024
129
- let [& foo ] = & mut [42 ];
130
- let _ : u8 = foo ;
131
- ```
132
-
133
133
# Reference-level explanation
134
134
[ reference-level-explanation ] : #reference-level-explanation
135
135
@@ -201,14 +201,18 @@ let _: u8 = a;
201
201
// let &b = 17; // ERROR
202
202
```
203
203
204
- If the default binding mode is ` ref ` , then ` &mut ` patterns are forbidden. If it
205
- is by-value, then they have the same effect as on older editions.
204
+ If the default binding mode is ` ref ` , then ` &mut ` patterns will not be able to
205
+ match against it, so they will match structurally instead (preserving the
206
+ binding mode).
206
207
207
208
``` rust
208
209
// ! Edition ≥ 2024
209
- // let [&mut x] = &[&mut 42]; // ERROR
210
+ let [& mut x ] = & [& mut 42 ];
211
+ let _ : & u8 = x ;
210
212
```
211
213
214
+ ` &mut ` patterns are otherwise unchanged.
215
+
212
216
``` rust
213
217
// ! All editions
214
218
@@ -490,25 +494,26 @@ fully compatible with proposals for "deref patterns", including allowing
490
494
question that would need to be resolved is whether and how deref patterns
491
495
(explicit or implicit) affect the default binding mode.
492
496
493
- ## Matching ` &mut ` behind ` & `
497
+ ## Matching ` &mut ` directly behind ` & `
494
498
495
499
There is one notable situation where match ergonomics cannot be used, and
496
500
explicit ` ref ` is required. This happens where ` &mut ` is nested behind ` & ` :
497
501
498
502
``` rust
499
503
// No way to avoid the `ref`, even with this RFC
500
- let & [ & mut ref x ] = & [ & mut 42 ] ; // x: &i32
504
+ let && mut ref x = && mut 42 ; // x: &i32
501
505
```
502
506
503
507
There are two strategies we could take to support this:
504
508
505
- - ` &mut ` patterns could match "behind" ` & ` . For example, in ` let [&mut x] = &[&mut 42]; ` ,
506
- the ` &mut ` pattern would match the ` &mut ` reference in the scrutinee, leaving
507
- ` & ` to be inherited and resulting in ` x: &i32 ` .
509
+ - ` &mut ` patterns could “strip off” outer ` & ` . For example, in
510
+ ` let &mut x = &&mut 42; ` , the ` &mut ` pattern would match the ` &mut ` reference
511
+ in the scrutinee, leaving ` & ` to be inherited and resulting in ` x: &i32 ` .
508
512
- This may not extend gracefully to future language features (partial borrows,
509
- for example) as it relies on reference types forming a total order.
513
+ for example) as it potentially relies on reference types forming a total
514
+ order.
510
515
- The compiler could insert ` &mut ref ` in front of identifier patterns of type
511
- ` &mut ` that are behind an ` & ` pattern. For example, ` let &[x] = &[ &mut 42]; `
512
- would be transformed into ` let &[ &mut ref x] = &[ &mut 42] ; ` .
516
+ ` &mut ` that are behind an ` & ` pattern. For example, ` let &x = &&mut 42; ` would
517
+ be transformed into ` let &&mut ref x = &&mut 42; ` .
513
518
- The full desugaring would be more complicated, as it would need to handle
514
519
` @ ` patterns.
0 commit comments