提问人:Plegeus 提问时间:6/26/2023 最后编辑:Plegeus 更新时间:6/26/2023 访问量:87
如果一个变量在 Rust 中 main 函数的末尾被删除,为什么它不会被借用用于静态?
Why is a variable not borrowed for static if it is dropped at the end of the main function in Rust?
问:
我正在尝试传递一个捕获局部变量的闭包:
fn main() {
/* snip */
let COINT = some_function_call();
/* snip */
hermes.with_task(
u32::MAX,
1,
1,
Box::new(
|hermes| {
let mut rng = rand::thread_rng();
Command::SpawnCommand(
COIN,
Vec3::new(
rng.gen_range(-WORLD_SIZE..WORLD_SIZE),
5.5,
rng.gen_range(-WORLD_SIZE..WORLD_SIZE)
)
)
}
)
);
/* snip */
}
它是变量 COIN。
The error I receive is as follows:
error[E0597]: `COIN` does not live long enough
--> src\main.rs:250:11
|
246 | / Box::new(
247 | | |hermes| {
| | -------- value captured here
248 | | let mut rng = rand::thread_rng();
249 | | Command::SpawnCommand(
250 | | COIN,
| | ^^^^ borrowed value does not live long enough
... |
257 | | }
258 | | )
| |_____- cast requires that `COIN` is borrowed for `'static`
...
272 | }
| - `COIN` dropped here while still borrowed
方法with_task定义如下:
impl Hermes {
/* snip */
pub fn with_task<'a: 'static>(&mut self, repeat: u32, span: u32, offset: u32, f: Box<dyn Fn(&Hermes) -> Command + 'a>) -> usize {
let t = TaskInfo {
task: f,
repeat,
span,
counter: offset,
};
self.tasks.push(t);
self.tasks.len() - 1
}
/* snip */
}
最让我困惑的是错误中的这一行:
272 | }
| - `COIN` dropped here while still borrowed
这正是主要功能结束的地方,我只是不明白问题出在哪里。
谢谢你帮助我:)
编辑下面是一个可重现的示例:
struct Foo {
tasks: Vec<Box<dyn Fn(&Foo) -> Command>>,
}
impl Foo {
fn with_task(&mut self, f: Box<dyn Fn(&Foo) -> Command>) {
self.tasks.push(f);
}
}
enum Command {
Com1(usize),
}
fn main() {
let VAR: usize = 0;
let mut foo = Foo {
tasks: Vec::new(),
};
foo.with_task(Box::new(
|foo| {
Command::Com1(VAR)
}
));
}
答: 暂无答案
评论
main
static
Box::new(move |hermes| {
Box::new(|hermes| {
COINT
std::thread::scope
move
Foo
Rc
Box<dyn(&Foo) -> Command + 'static>
。在引入显式生存期绑定后,代码工作正常,即使没有 .Box<dyn Fn(&Foo) -> Command>
move