This my question was answered.
But when I modified the code a little, it does not work again with a compilation error similar to the error in the previous question. What is my error now?
use std::thread;
#[derive(Clone)]
struct Params<'a> {
x: &'a i32,
}
struct State<'a> {
params: &'a Params<'a>,
y: i32,
}
impl<'a> State<'a> {
fn new(params: &'a Params<'a>) -> Self {
State {
params,
y: 0,
}
}
fn start(&mut self) -> thread::JoinHandle<()> {
let params = self.params.clone();
thread::spawn(move || { params; /* ... */ })
}
}
error[E0495]: cannot infer an appropriate lifetime due to conflicting requirements
--> src/lib.rs:21:34
|
21 | let params = self.params.clone();
| ^^^^^
|
note: first, the lifetime cannot outlive the lifetime `'a` as defined on the impl at 13:6...
--> src/lib.rs:13:6
|
13 | impl<'a> State<'a> {
| ^^
note: ...so that the types are compatible
--> src/lib.rs:21:34
|
21 | let params = self.params.clone();
| ^^^^^
= note: expected `&Params<'_>`
found `&Params<'a>`
= note: but, the lifetime must be valid for the static lifetime...
note: ...so that the type `[closure@src/lib.rs:22:23: 22:52 params:Params<'_>]` will meet its required lifetime bounds
--> src/lib.rs:22:9
|
22 | thread::spawn(move || { params; /* ... */ })
| ^^^^^^^^^^^^^
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…