Skip to content

Commit 4dfaa4f

Browse files
authored
Merge pull request #1702 from rust-lang/TC/adjust-for-rust-2024-match-ergonomics-reservations
Document Rust 2024 match ergonomics reservations
2 parents 1dffb2b + 0a608b4 commit 4dfaa4f

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)