Skip to content

Slice patterns don't work with non-Copy types #50764

Closed
@cake4289

Description

@cake4289

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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions