[英]Why move closure in rust can't move the ownership of a referenced variable?
这是示例
fn call_with_f<F>(f:F) where F: FnOnce(){
f();
}
struct Vec2{
x:i32,
y:i32
}
impl Vec2{
fn new(x:i32, y:i32) -> Self{
Self{
x,
y
}
}
fn x(&self) -> &i32{
&self.x
}
}
fn main() {
let v = Vec2::new(1,2);
let rx = v.x();
call_with_f(move||{
println!("{}", v.x());
println!("{}", rx);
});
}
编译器给出
error[E0505]: cannot move out of `v` because it is borrowed
--> src\main.rs:88:14
|
87 | let rx = v.x();
| - borrow of `v` occurs here
88 | call_with_f(move||{
| ^^^^^^ move out of `v` occurs here
89 | println!("{}", v.x());
| - move occurs due to use in closure
90 | println!("{}", rx);
| -- borrow later captured here by closure
我知道编译器的抱怨,但这种情况有时是必要的。 在一个非常复杂的
在这种情况下,在给闭包之前需要做很多工作,作为中间变量的引用可以简化准备工作。 v
和rv
在开始时具有相同的生命周期,根据移动闭包的语义,它们在移动后也保持相同的生命周期。 为什么?
您不能移动借入的值。 时期。
此外,这样的闭包不能(安全地)存在:它需要存储拥有的值和对它(部分)的引用,即它是自引用的。 请参阅闭包中引用的生命周期。
根据您的用例,可能有各种解决方案,从最好到最差排序:
'static
闭包”。 如果是这种情况,您可能会发现作用域线程很有帮助。owning_ref
(unsound!) 或ourboros
(no known sound hole IIRC) 之类的 crate 来创建自引用闭包。 您可能需要手动(至少部分地)对关闭进行脱糖。unsafe
的代码来创建自引用闭包。 警告:除非您真的知道自己在做什么,否则不要这样做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.