Tin*_*Lin 6 reference rust type-annotation borrow-checker
当我遇到这个奇怪的案例时,我正在学习 Rust 以及所有参考资料和借用的东西:
fn main() {
let mut n = 42u32;
let ref1 = &mut n;
let ref2 = ref1; // line 5
ref2;
ref1; // line 8
}
Run Code Online (Sandbox Code Playgroud)
该代码片段无法编译,因为ref1它已移至第 5 行,并且不能在第 8 行中使用,据我所知。
但是当我向 ref2 添加类型注释(这似乎完全无关,并且根本不会影响任何内容,因为rust-analyzer报告的类型完全相同)时:
fn main() {
let mut n = 42u32;
let ref1 = &mut n;
let ref2: &mut u32 = ref1;
// ++++++++++
ref2;
ref1;
}
Run Code Online (Sandbox Code Playgroud)
它突然编译了!(你可以在游乐场尝试一下)
到底是怎么回事?我目前使用的是 rustc 1.65.0,这是某种错误吗?或者我错过了什么?
我在编译器资源管理器中使用不同版本的 rustc 进行了尝试,似乎 1.36.0 是第一个成功编译第二个片段的版本。所以我检查了公告,注意到他们在该版本中引入了名为“非词汇生命周期”的东西,我不确定这是否相关,但我注意到 rustc 1.35.0 的错误消息是:
error[E0505]: cannot move out of `ref1` because it is borrowed -->
<source>:9:5
|
6 | let ref2: &mut u32 = ref1;
| ---- borrow of `*ref1` occurs here
...
9 | ref1;
| ^^^^ move out of `ref1` occurs here
error: aborting due to previous error
Run Code Online (Sandbox Code Playgroud)
ref1这与第一个片段的错误不同,第一个片段基本上表明您在移动后无法使用。我该如何理解这一点?