rust - Moving structure containing reference into static closure doesn't work
问题描述
I'm creating a structure containing a reference and a function that gets a closure with 'static
lifetime:
pub struct RefStruct<'a> {
pub mref: &'a u32,
}
fn run<F>(mut f: F)
where
F: FnMut() -> () + 'static,
{
f();
}
I create an integer num
which lives throughout the execution of main
:
fn main() {
let num = 32;
...
}
Now I want to create a struct
referencing it. If I create it inside the 'static
closure, it works fine:
fn main() {
let num = 32;
run(move || {
let rstruct = RefStruct { mref: &num }; // Ok!
print!("{}", rstruct.mref);
});
}
However, if I create the struct
in the main
function scope and then move it into the closure it doesn't:
fn main() {
let num = 32;
let rstruct = RefStruct { mref: &num };
run(move || {
print!("{}", rstruct.mref);
});
}
I get
error[E0597]: `num` does not live long enough
--> src/main.rs:15:37
|
15 | let rstruct = RefStruct { mref: &num };
| ^^^^ borrowed value does not live long enough
16 | / run(move || {
17 | | print!("{}", rstruct.mref);
18 | | });
| |______- argument requires that `num` is borrowed for `'static`
19 | }
| - `num` dropped here while still borrowed
error: aborting due to previous error
However, it seems to me that the two scopes should be identical.
解决方案
基本上在第一种情况下,num
在闭包中被引用,因此它被捕获并在闭包内移动。它变成'static
并且引用是有效的。
在第二种情况下,num
不被捕获,因为标识符从未在闭包中使用。因此,引用变为静态,而num
不是。
推荐阅读
- html - 打开虚拟键盘时,iOS 上的 Safari 会滚动到元素之外
- php - php执行链接页面
- javascript - 如何使用 javascript 和 html 复制不同的 div?
- python - 将 DataFrame 插入/附加到 MultiIndex DataFrame
- typescript - 在打字稿中扩展枚举
- angular - 无法读取未定义的属性“currentValue”
- python - 以逗号分隔数千个 Pandas DataFrame
- java - 在 Linux 上使用 Java CIFS 客户端库发送文件
- c# - C# 过滤 XML 的属性名称
- django - 如何在 Django 中使用 UNION + 添加假列