Skip to content

Commit 07b35d8

Browse files
Add more examples to "no ref mut behind &" rationale
1 parent 099b71f commit 07b35d8

File tree

1 file changed

+42
-13
lines changed

1 file changed

+42
-13
lines changed

text/3627-match-ergonomics-2024.md

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -330,30 +330,59 @@ anyway is that the current behavior is unintuitive and surprising for users.
330330

331331
### We can't delay this choice
332332

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
337334

338335
```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
340352
// `&mut` in pattern needs to match against either:
341353
// - `&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 `&`)
344355
```
345356

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+
```
347363

348364
### Makes behavior more consistent
349365

350366
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+
352374
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.
357386

358387
### Ensures that a desirable property is preserved
359388

0 commit comments

Comments
 (0)