في المثال التالي ، يقترض الإغلاق القيمة لفترة أطول مما ينبغي:
fn main () {
let f = |y| { println!("{}", y); };
let x = 4u8;
f(&x);
}
مما يسبب خطأ في الترجمة:
error: `x` does not live long enough
--> ./test.rs:5:1
|
4 | f(&x);
| - borrow occurs here
5 | }
| ^ `x` dropped here while still borrowed
|
= note: values in a scope are dropped in the opposite order they are created
ومع ذلك ، إذا أعلنا عن نوع الوسيطة صراحة ، فإنه يعمل كما هو متوقع:
fn main () {
let f = |y: &u8| { println!("{}", y); };
let x = 4u8;
f(&x);
}
لست متأكدًا مما إذا كان هذا خطأ أو سلوكًا متوقعًا. ولا يمكنني العثور على تفسير لهذا التأثير في الوثائق.
rustc --version --verbose
:
rustc 1.17.0-nightly (24a70eb59 2017-02-09)
binary: rustc
commit-hash: 24a70eb598a76edb0941f628a87946b40f2a1c83
commit-date: 2017-02-09
host: x86_64-unknown-linux-gnu
release: 1.17.0-nightly
LLVM version: 3.9
فقط لإسقاط مصطلح لمحركات البحث: استكشاف هذا اليوم في منتدى المستخدمين ، لدي انطباع قوي بأن السبب |x: &u8|
قد يكون مرتبطًا بكونه بمثابة تلميح لـ HRTB.
من المحتمل أن يكون ذا صلة: لقد صادفت هذا أيضًا (على وجه التحديد مثال huon في أكتوبر) ، والذي ينكسر بشكل غريب في الاتجاه الآخر ؛ أي أنه يعمل فقط _ بدون _ التعليق التوضيحي الصريح: https://github.com/rust-lang/rust/issues/22557
الفرز: تجمع الآن في 2018.
أخطاء 2015:
error[E0597]: `x` does not live long enough
--> src/main.rs:4:8
|
4 | f(&x);
| ^ borrowed value does not live long enough
5 | }
| - `x` dropped here while still borrowed
|
= note: values in a scope are dropped in the opposite order they are created
rustc: 1.32.0
يتم الإغلاق لأن هذا لم يعد يمثل مشكلة ليلا في أي من الإصدارين
التعليق الأكثر فائدة
الفرز: تجمع الآن في 2018.
أخطاء 2015:
rustc: 1.32.0