Skip to content

Commit 0a608b4

Browse files
committed
Document Rust 2024 match ergonomics reservations
We're adopting two changes to match ergonomics in Rust 2024 that have the effect of reserving language space: - Rule 1C: When the DBM is not `move` (whether or not behind a reference), writing `mut`, `ref`, or `ref mut` on a binding is an error. - Rule 2C: Reference patterns can only match against references in the scrutinee when the DBM is `move`. Here, we document those changes in the Reference and describe the differences between editions.
1 parent 1dffb2b commit 0a608b4

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/patterns.md

+18
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,24 @@ References will set the default binding mode to `ref`.
265265
Mutable references will set the mode to `ref mut` unless the mode is already `ref` in which case it remains `ref`.
266266
If the automatically dereferenced value is still a reference, it is dereferenced and this process repeats.
267267

268+
The binding pattern may only explicitly specify a `ref` or `ref mut` binding mode, or specify mutability with `mut`, when the default binding mode is "move". For example, these are not accepted:
269+
270+
```rust,edition2024,compile_fail
271+
let [mut x] = &[()]; //~ ERROR
272+
let [ref x] = &[()]; //~ ERROR
273+
let [ref mut x] = &mut [()]; //~ ERROR
274+
```
275+
276+
> **Edition differences**: Before the 2024 edition, bindings could explicitly specify a `ref` or `ref mut` binding mode even when the default binding mode was not "move", and they could specify mutability on such bindings with `mut`. In these editions, specifying `mut` on a binding set the binding mode to "move" regardless of the current default binding mode.
277+
278+
Similarly, a reference pattern may only appear when the default binding mode is "move". For example, this is not accepted:
279+
280+
```rust,edition2024,compile_fail
281+
let [&x] = &[&()]; //~ ERROR
282+
```
283+
284+
> **Edition differences**: Before the 2024 edition, reference patterns could appear even when the default binding mode was not "move", and had both the effect of matching against the scrutinee and of causing the default binding mode to be reset to "move".
285+
268286
Move bindings and reference bindings can be mixed together in the same pattern.
269287
Doing so will result in partial move of the object bound to and the object cannot be used afterwards.
270288
This applies only if the type cannot be copied.

0 commit comments

Comments
 (0)