Hello,
I have this extremely simplified Example that looks as follows:
use std::thread::spawn;
struct Service {}
impl Service {
fn run_tasks(&self) {
for i in 1..5 {
spawn(move || self.transition(i));
}
}
fn transition(&self, i: i32) {
println!("Hello {}", i);
}
}
fn main() {
let service = Service {};
service.run_tasks();
}
That fails to compile with the following error:
error[E0495]: cannot infer an appropriate lifetime due to conflicting requirements
--> src/main.rs:8:19
|
8 | spawn(move || self.transition(i));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 6:5...
--> src/main.rs:6:5
|
6 | / fn run_tasks(&self) {
7 | | for i in 1..5 {
8 | | spawn(move || self.transition(i));
9 | | }
10| | }
| |_____^
= note: ...so that the types are compatible:
expected &Service
found &Service
= note: but, the lifetime must be valid for the static lifetime...
note: ...so that the type `[closure@src/main.rs:8:19: 8:45 self:&Service, i:i32]` will meet its required lifetime bounds
--> src/main.rs:8:13
|
8 | spawn(move || self.transition(i));
| ^^^^^
What i am trying to do is create a Service
instance which would initialize a state machine and would be responsible for running a bunch of threads (or futures) to perform certain tasks. Once each of those tasks completes, it would transition the state machine
So my understanding is that the closure would capture the ownership of variables from the scope in which the closure is created. Rust wants to make sure that self
will love as long as the thread is running which is why it is requiring the 'static
lifetime. Provided that self
is a shared reference, i am not totally sure what to do about this.
I could add 'static
to self
in the function signature to be &'static self
but this will lead the variable to be a global one to satisfy the compiler.
How would i go about fixing my code in a way to satisfy the compiler?
Note: I am using tokio in my original code base but this exact error is what i am seeing.