线程寿命错误
Threading lifetime error
我正在尝试使用 Rust 中的并发实现游戏 "Tower of Hanoi"。老实说,上次我试图理解 Rust 的整个生命周期,但我还没有完成。这就是为什么我会遇到一些我不理解的奇怪的终生错误。首先,这是重要的代码
fn move_plate<'a>(stack_a: &'a mut Vec<i32>, stack_b: &'a mut Vec<i32>,
stack_c: &'a mut Vec<i32>, moves: &'a mut Vec<(i32, i32)>)
{
let mut moves1: Vec<(i32, i32)> = Vec::new();
let guard1 = Thread::scoped(
move || { move_plate(stack_a, stack_c, stack_b, (1, 3, 2), &mut moves1);
});
guard1.join().ok();
}
这里是错误
error: cannot infer an appropriate lifetime due to conflicting requirements
let guard1 = Thread::scoped(move || {
move_plate(height - 1, stack_a, stack_c, stack_b, (1, 3, 2), threads, depth + 1, &mut moves1);
});
note: first, the lifetime cannot outlive the expression at 93:25...
let guard1 = Thread::scoped(move || {
note: ...so that the declared lifetime parameter bounds are satisfied
let guard1 = Thread::scoped(move || {
note: but, the lifetime must be valid for the expression at 93:45...
let guard1 = Thread::scoped(move || {
move_plate(height - 1, stack_a, stack_c, stack_b, (1, 3, 2), threads, depth + 1, &mut moves1);
});
note: ...so type `closure[]` of expression is valid during the expression
let guard1 = Thread::scoped(move || {
move_plate(height - 1, stack_a, stack_c, stack_b, (1, 3, 2), threads, depth + 1, &mut moves1);
});
error: declared lifetime bound not satisfied
let guard1 = Thread::scoped(move || {
我知道我必须避免线程比函数长寿,否则对移动的引用就会消失。不过既然我加入了线程,那应该就可以了,不是吗?那时我错过了什么?
如果有人能帮助我,那就太好了,我只是习惯了那种很酷(但很复杂)的东西
这是 Rust 类型系统的已知限制。目前 Rust 仅在数据满足 Send
边界时才允许在线程之间发送数据,并且 Send
意味着 'static
- 也就是说,可以跨线程边界发送的唯一引用是 'static
个。
有 an RFC 部分解除了此限制,允许跨任务发送非 'static
引用。我以为它已经被接受了,但事实并非如此(这很奇怪)。支持这种东西的 API 已经创建(这可能是你困惑的原因),但语言尚未调整。
我正在尝试使用 Rust 中的并发实现游戏 "Tower of Hanoi"。老实说,上次我试图理解 Rust 的整个生命周期,但我还没有完成。这就是为什么我会遇到一些我不理解的奇怪的终生错误。首先,这是重要的代码
fn move_plate<'a>(stack_a: &'a mut Vec<i32>, stack_b: &'a mut Vec<i32>,
stack_c: &'a mut Vec<i32>, moves: &'a mut Vec<(i32, i32)>)
{
let mut moves1: Vec<(i32, i32)> = Vec::new();
let guard1 = Thread::scoped(
move || { move_plate(stack_a, stack_c, stack_b, (1, 3, 2), &mut moves1);
});
guard1.join().ok();
}
这里是错误
error: cannot infer an appropriate lifetime due to conflicting requirements
let guard1 = Thread::scoped(move || {
move_plate(height - 1, stack_a, stack_c, stack_b, (1, 3, 2), threads, depth + 1, &mut moves1);
});
note: first, the lifetime cannot outlive the expression at 93:25...
let guard1 = Thread::scoped(move || {
note: ...so that the declared lifetime parameter bounds are satisfied
let guard1 = Thread::scoped(move || {
note: but, the lifetime must be valid for the expression at 93:45...
let guard1 = Thread::scoped(move || {
move_plate(height - 1, stack_a, stack_c, stack_b, (1, 3, 2), threads, depth + 1, &mut moves1);
});
note: ...so type `closure[]` of expression is valid during the expression
let guard1 = Thread::scoped(move || {
move_plate(height - 1, stack_a, stack_c, stack_b, (1, 3, 2), threads, depth + 1, &mut moves1);
});
error: declared lifetime bound not satisfied
let guard1 = Thread::scoped(move || {
我知道我必须避免线程比函数长寿,否则对移动的引用就会消失。不过既然我加入了线程,那应该就可以了,不是吗?那时我错过了什么? 如果有人能帮助我,那就太好了,我只是习惯了那种很酷(但很复杂)的东西
这是 Rust 类型系统的已知限制。目前 Rust 仅在数据满足 Send
边界时才允许在线程之间发送数据,并且 Send
意味着 'static
- 也就是说,可以跨线程边界发送的唯一引用是 'static
个。
有 an RFC 部分解除了此限制,允许跨任务发送非 'static
引用。我以为它已经被接受了,但事实并非如此(这很奇怪)。支持这种东西的 API 已经创建(这可能是你困惑的原因),但语言尚未调整。