@@ -330,30 +330,59 @@ anyway is that the current behavior is unintuitive and surprising for users.
330
330
331
331
### We can't delay this choice
332
332
333
- Note that while this is not a breaking change for edition 2021 and below, it
334
- * would be breaking* to adopt the rest of this RFC without this change, and then
335
- later adopt this change alone. Specifically, pattern matches like the following
336
- would break:
333
+ #### Patterns that work only with this rule
337
334
338
335
``` rust
339
- let & [[& mut a ]] = & [& mut [42 ]];
336
+ // ! All editions: works only with this rule
337
+ let & [[a ]] = & [& mut [42 ]]; // x: &i32
338
+ ```
339
+
340
+ ``` rust
341
+ // ! All editions: works with or without this rule (alternative to above)
342
+ // Note the explicit `ref`, we must abandon match ergonomics
343
+ let & [[ref a ]] = & [& mut [42 ]]; // x: &i32
344
+ let & [& mut [ref a ]] = & [& mut [42 ]]; // x: &i32
345
+ ```
346
+
347
+ #### Patterns that work only without this rule
348
+
349
+ ``` rust
350
+ // ! Edition ≥ 2024: works only without this rule
351
+ let & [[& mut a ]] = & [& mut [42 ]]; // x: i32
340
352
// `&mut` in pattern needs to match against either:
341
353
// - `&mut` in value at same position (there is none, so not possible)
342
- // - inherited `&mut` (which the "never set default binding mode to `ref mut` behind `&`" rule
343
- // downgrades to `&`)
354
+ // - inherited `&mut` (which the rule downgrades to `&`)
344
355
```
345
356
346
- Therefore, we cannot delay a decision on this matter.
357
+ ``` rust
358
+ // ! Edition ≥ 2024: works with or without this rule (alternatives to above)
359
+ // No need to abandon match ergonomics
360
+ let & [[& a ]] = & [& mut [42 ]]; // x: i32
361
+ let & [& mut [& a ]] = & [& mut [42 ]]; // x: i32
362
+ ```
347
363
348
364
### Makes behavior more consistent
349
365
350
366
On all editions, when a structure pattern peels off a shared reference and the
351
- default binding mode is already ` ref mut ` , the binding mode gets set to ` ref ` .
367
+ default binding mode is already ` ref mut ` , the binding mode gets set to ` ref ` :
368
+
369
+ ``` rust
370
+ // ! All editions
371
+ let [a ] = & mut & [42 ]; // x: &i32
372
+ ```
373
+
352
374
But when the binding mode is set to ` ref ` , and a mutable reference is peeled
353
- off, the binding mode remains ` ref ` . In other words, immutability usually takes
354
- precedence over mutability. This change, in addition to being generally useful,
355
- makes the match ergonomics rules more consistent by ensuring that immutability
356
- * always* takes precedence over mutability.
375
+ off, the binding mode remains ` ref ` :
376
+
377
+ ``` rust
378
+ // ! All editions
379
+ let [a ] = && mut [42 ]; // x: &i32
380
+ ```
381
+
382
+ In other words, immutability usually takes precedence over mutability. This
383
+ change, in addition to being generally useful, makes the match ergonomics rules
384
+ more consistent by ensuring that immutability * always* takes precedence over
385
+ mutability.
357
386
358
387
### Ensures that a desirable property is preserved
359
388
0 commit comments