Rust: No se puede desestructurar una estructura en caja sin una unión let intermedia

Creado en 12 ene. 2018  ·  3Comentarios  ·  Fuente: rust-lang/rust

Este código no se puede compilar:

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

Sin embargo, la inserción de un enlace de let intermedio entre el derecho de la caja y la desestructuración lo arregla:

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

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

Esto se siente como un error.

Versiones afectadas:

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

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

C-bug T-compiler

Comentario más útil

También ocurre con los patrones de caja.

#![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;
}

Todos 3 comentarios

También ocurre con los patrones de caja.

#![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: ¡esto está arreglado!

¿Fue útil esta página
0 / 5 - 0 calificaciones