Rust: Can't destructure a boxed struct without an intermediate let binding

Created on 12 Jan 2018  ·  3Comments  ·  Source: rust-lang/rust

This code fails to compile:

struct Foo;

struct Bar {
    a: Foo,
    b: Foo,
}

fn main() {
    let bar = Box::new(Bar { a: Foo, b: Foo });

    let Bar { a, b } = *bar;
}
error[E0382]: use of moved value: `bar`
  --> x.rs:11:18
   |
11 |     let Bar { a, b } = *bar;
   |               -  ^ value used here after move
   |               |
   |               value moved here
   |
   = note: move occurs because `bar.a` has type `Foo`, which does not implement the `Copy` trait

However inserting an intermediate let binding in between the box deref and the destructure fixes it:

fn main() {
    let bar = Box::new(Bar { a: Foo, b: Foo });

    let Bar { a, b } = { let intermediate = *bar; intermediate };
}

This feels like a bug.

Affected versions:

  • rustc 1.23.0 (766bd11c8 2018-01-01)

  • rustc 1.25.0-nightly (73ac5d6a8 2018-01-11)

C-bug T-compiler

Most helpful comment

Also happens with box patterns.

#![feature(box_patterns)]

struct Foo;

struct Bar {
    a: Foo,
    b: Foo,
}

fn main() {
    let bar = Box::new(Bar { a: Foo, b: Foo });

    let box Bar { a, b } = bar;
}

All 3 comments

Also happens with box patterns.

#![feature(box_patterns)]

struct Foo;

struct Bar {
    a: Foo,
    b: Foo,
}

fn main() {
    let bar = Box::new(Bar { a: Foo, b: Foo });

    let box Bar { a, b } = bar;
}

Triage: this is fixed!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

thestinger picture thestinger  ·  234Comments

withoutboats picture withoutboats  ·  308Comments

cramertj picture cramertj  ·  512Comments

alexcrichton picture alexcrichton  ·  240Comments

Leo1003 picture Leo1003  ·  898Comments