Rust: لا يمكن إتلاف هيكل محاصر بدون ربط وسيط

تم إنشاؤها على ١٢ يناير ٢٠١٨  ·  3تعليقات  ·  مصدر: rust-lang/rust

فشل هذا الرمز في ترجمة:

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

ومع ذلك ، فإن إدخال ارتباط وسيط بين مربع deref والتدمير يعمل على إصلاحه:

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

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

هذا يبدو وكأنه خطأ.

الإصدارات المتأثرة:

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

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

C-bug T-compiler

التعليق الأكثر فائدة

يحدث أيضًا مع أنماط الصندوق.

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

ال 3 كومينتر

يحدث أيضًا مع أنماط الصندوق.

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

الفرز: هذا ثابت!

هل كانت هذه الصفحة مفيدة؟
0 / 5 - 0 التقييمات