Closed
Description
Slice patterns cannot be used to destructure arrays of non-Copy
types:
struct Struct;
fn main() {
let x = [Struct, Struct];
let [a, b] = x;
}
error[E0382]: use of moved value: `x[..]`
--> src/main.rs:5:13
|
5 | let [a, b] = x;
| - ^ value used here after move
| |
| value moved here
|
= note: move occurs because `x[..]` has type `Struct`, which does not implement the `Copy` trait
This makes array destructuring pretty redundant IMHO as moving out of a Copy
array could already be done through indexing; moving out of a non-Copy
array cannot be done but destructuring provides a safe syntax to do so.
On a related note, the following code fails to compile:
struct Struct;
fn main() {
let x = [Struct, Struct];
let a = x[0];
}
This should at the very least compile in the same way as
struct Struct;
fn main() {
let x = [Struct, Struct];
let [a, _] = x;
}
Which does compile correctly in rust 1.26.
Metadata
Metadata
Assignees
Labels
No labels