Description
The following code (playground link) attempts to move a struct while using a sub-binding to copy one of its fields:
#![feature(bindings_after_at)]
struct NonCopyStruct {
copy_field: u32,
}
fn foo(x: NonCopyStruct) {
let y @ NonCopyStruct { copy_field: z } = x;
}
On nightly, it produces two errors:
error[E0007]: cannot bind by-move with sub-bindings
--> src/lib.rs:8:9
|
8 | let y @ NonCopyStruct { copy_field: z } = x;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ binds an already bound by-move value by moving it
error[E0382]: use of moved value: `x`
--> src/lib.rs:8:41
|
7 | fn foo(x: NonCopyStruct) {
| - move occurs because `x` has type `NonCopyStruct`, which does not implement the `Copy` trait
8 | let y @ NonCopyStruct { copy_field: z } = x;
| --------------------------------^--
| | |
| | value used here after move
However, it would be sound to allow this, since it's equivalent to copying the field before or after moving the struct.
(There are no errors if the struct does implement Copy
.)