Rust: Impossible de déstructurer une structure en boîte sans liaison let intermédiaire

Créé le 12 janv. 2018  ·  3Commentaires  ·  Source: rust-lang/rust

Ce code ne parvient pas à compiler :

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

Cependant l'insertion d'une liaison intermédiaire entre la case deref et la destructure la fixe :

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

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

Cela ressemble à un bug.

Versions concernées :

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

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

C-bug T-compiler

Commentaire le plus utile

Se produit également avec les modèles de boîte.

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

Tous les 3 commentaires

Se produit également avec les modèles de boîte.

#![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 : c'est corrigé !

Cette page vous a été utile?
0 / 5 - 0 notes