我正在尝试编写一个二叉树,它可以在线程之间传递,而不必每次都被复制。我很难理解如何在Rust的生命周期限制下做到这一点。
use std::thread::spawn;
#[derive(Debug)]
struct Node<'a> {
left: &'a i32,
right: &'a i32,
}
fn main() {
let l = 3;
let r = 4;
let n = Node {
left: &l,
right: &r,
};
spawn(|| {
println!("{:?}", n);
});
}
error[E0597]: `l` does not live long enough
--> src/main.rs:13:15
|
13 | left: &l,
| ^^ borrowed value does not live long enough
...
17 | / spawn(|| {
18 | | println!("{:?}", n);
19 | | });
| |______- argument requires that `l` is borrowed for `'static`
20 | }
| - `l` dropped here while still borrowed
error[E0597]: `r` does not live long enough
--> src/main.rs:14:16
|
14 | right: &r,
| ^^ borrowed value does not live long enough
...
17 | / spawn(|| {
18 | | println!("{:?}", n);
19 | | });
| |______- argument requires that `r` is borrowed for `'static`
20 | }
| - `r` dropped here while still borrowed
error[E0373]: closure may outlive the current function, but it borrows `n`, which is owned by the current function
--> src/main.rs:17:11
|
17 | spawn(|| {
| ^^ may outlive borrowed value `n`
18 | println!("{:?}", n);
| - `n` is borrowed here
|
note: function requires argument type to outlive `'static`
--> src/main.rs:17:5
|
17 | / spawn(|| {
18 | | println!("{:?}", n);
19 | | });
| |______^
help: to force the closure to take ownership of `n` (and any other referenced variables), use the `move` keyword
|
17 | spawn(move || {
| ^^^^^^^
我理解为什么它会认为它们活得不够长,但我应该如何重组它才能让它们活得足够长呢?
发布于 2014-10-04 15:19:14
在您的情况下,使用在main()
函数中定义的变量引用创建一个Node
对象,并将其传递给一个新线程。问题是你完全不能保证这个线程会在你的main()
的线程之前完成,并且你冒着在引用之前原始变量超出作用域的风险,因此出现了错误。
考虑到您希望构建一个树,我认为最简单的解决方法是让您的Node
类型拥有它的字段,如下所示:
#[derive(Debug)]
struct Node {
left: i32,
right: i32,
}
这样你就不会再有终生的问题了。
要在不复制的情况下在多个线程之间共享它,可以使用std::sync::Arc
包装器;它就是为了这个目的。你可以这样做:
use std::sync::Arc;
fn main() {
let n = Arc::new(Node { left: 3, right: 4 });
for _ in 0..10 {
let n_thread = n.clone();
spawn(move || {
println!("{:?}", n_thread);
});
}
}
如果您还需要授予其他线程对Node
的写访问权限,您可能会希望将其包装在Mutex
或RwLock
中,将所有内容都保留在Arc
中。
https://stackoverflow.com/questions/26190213
复制相似问题