Rei*_*aux 2 concurrency lifetime rust
我正在尝试使用Rust中的并发来实现游戏"River 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();
}
Run Code Online (Sandbox Code Playgroud)
这是错误
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 || {
Run Code Online (Sandbox Code Playgroud)
我明白我必须避免线程超过函数,因为否则对移动的引用将会消失.但既然我加入了这个帖子,那应该没问题,不应该吗?那时我错过了什么?如果有人可以帮助我,那真的很好,我只是习惯那种很酷(但很复杂)的东西
| 归档时间: |
|
| 查看次数: |
108 次 |
| 最近记录: |