Skip to content

Commit d623f9c

Browse files
& matches &mut on all editions
1 parent 04501bc commit d623f9c

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

text/3627-match-ergonomics-2024.md

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Various changes to the match ergonomics rules:
1212
references.
1313
- On edition ≥ 2024, `mut` on an identifier pattern does not force its binding
1414
mode to by-value.
15-
- On edition ≥ 2024, `&` patterns can match against `&mut` references.
15+
- On all editions, `&` patterns can match against `&mut` references.
1616
- On all editions, the binding mode can no longer ever be implicitly set to
1717
`ref mut` behind an `&` pattern.
1818

@@ -34,11 +34,11 @@ let (x, mut y) = &(true, false);
3434
let _: (&bool, bool) = (x, y);
3535
```
3636

37-
## Can't cancel out an inherited reference
37+
## Cant cancel out an inherited reference
3838

3939
`&` and `&mut` patterns must correspond with a reference in the same position in
4040
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
4242
(<https://users.rust-lang.org/t/reference-of-tuple-and-tuple-of-reference/91713/6>,
4343
<https://users.rust-lang.org/t/cannot-deconstruct-reference-inside-match-on-reference-why/92147>,
4444
<https://github.com/rust-lang/rust/issues/50008>,
@@ -71,8 +71,8 @@ Match ergonomics works a little differently in edition 2024 and above.
7171

7272
## `mut` no longer strips the inherited reference
7373

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.
7676

7777
```rust
7878
//! Edition ≥ 2024
@@ -115,15 +115,21 @@ let _: &u8 = x;
115115

116116
## `&` matches against `&mut`
117117

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).
120120

121121
```rust
122-
//! Edition ≥ 2024
122+
//! All editions
123123
let &foo = &mut 42;
124124
let _: u8 = foo;
125125
```
126126

127+
```rust
128+
//! Edition ≥ 2024
129+
let [&foo] = &mut [42];
130+
let _: u8 = foo;
131+
```
132+
127133
# Reference-level explanation
128134
[reference-level-explanation]: #reference-level-explanation
129135

@@ -140,12 +146,12 @@ instead, `mut` on a binding with a by-reference binding mode is an error.
140146
// let [mut a] = &[42]; //ERROR
141147
```
142148

143-
## Edition 2024: `&` patterns can match against `&mut` references
149+
## All editions: `&` patterns can match against `&mut` references
144150

145151
`&` patterns can match against `&mut` references.
146152

147153
```rust
148-
//! Edition ≥ 2024
154+
//! All editions
149155
let &foo = &mut 42;
150156
let _: u8 = foo;
151157
```
@@ -328,7 +334,7 @@ anyway is that the current behavior is unintuitive and surprising for users.
328334

329335
## Never setting default binding mode to `ref mut` behind `&`
330336

331-
### We can't delay this choice
337+
### We cant delay this choice
332338

333339
#### Patterns that work only with this rule
334340

@@ -423,14 +429,14 @@ There are several motivations for allowing this:
423429
- It makes refactoring less painful. Sometimes, one is not certain whether an
424430
unfinished API will end up returning a shared or a mutable reference. But as
425431
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 doesnt matter either way, as `&mut` implicitly reborrows
427433
as `&` in many situations. Pattern matching is currently one of the most
428434
prominent exceptions to this, and match ergonomics magnifies the pain because
429435
a reference in one part of the pattern can affect the binding mode in a
430436
different, faraway location[^nrmba]. If patterns can be written to always use
431437
`&` unless mutation is required, then the amount of editing necessary to
432438
perform various refactors is lessened.
433-
- It's intuitive. `&mut` is strictly more powerful than `&`. It's conceptually a
439+
- Its intuitive. `&mut` is strictly more powerful than `&`. Its conceptually a
434440
subtype, and even if not implemented that way[^sub], coercions mean it often
435441
feels like one in practice.
436442

@@ -478,7 +484,7 @@ concerns with certain proposals for "deref patterns".
478484

479485
## Deref patterns
480486

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
482488
fully compatible with proposals for "deref patterns", including allowing
483489
`&`/`&mut` patterns to match against types implementing `Deref`/`DerefMut`. One
484490
question that would need to be resolved is whether and how deref patterns

0 commit comments

Comments
 (0)