Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
cake4289 opened this issue May 15, 2018 · 2 comments
Closed

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

cake4289 opened this issue May 15, 2018 · 2 comments

Comments

@cake4289
Copy link

cake4289 commented May 15, 2018

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.

@csmoe
Copy link
Member

csmoe commented May 15, 2018

Duplicate of #26736

@csmoe csmoe marked this as a duplicate of #26736 May 15, 2018
@csmoe csmoe closed this as completed May 15, 2018
@vandenheuvel
Copy link
Contributor

I'm not sure that this is really a duplicate, as #26736 is now resolved but the above example fails to compile on 1.45.0-nightly (2020-05-27 664fcd3f046e2a682460).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants