The exact error from my minimal reproducible example:
cannot infer an appropriate lifetime due to conflicting requirements
note: ...so that the types are compatible:
expected &test_struct
found &test_struct
note: but, the lifetime must be valid for the static lifetime...rustc(E0495)
I get this error in a much larger project as I was trying to thread calls to a function of a struct.
Here's the code that is a minimal reproducible example, producing the same error:
use std::thread;
pub struct test_struct {}
impl test_struct {
fn inner_test_fn(&self) -> u32 {
return 4u32;
}
pub fn outer_test_fn(&self) -> u32 {
let mut threads = vec![];
let mut total = 0u32;
for i in 0..4 {
threads.push(thread::spawn(move || {
let out = self.inner_test_fn();
total += out;
}));
}
for thread in threads {
thread.join();
}
return total;
}
}
fn main() {
let struct_holder = test_struct{};
let x = struct_holder.outer_test_fn();
}
I am almost certain the error is because of self.inner_test_fn();
but after this point I'm at a loss.
I have and am looking up and reading more about lifetimes right now, but I can't quite figure this out yet.
I am aware of other questions around the same error, but both questions and answers to these are far more complex and difficult to understand than my simple example.
Any help would be greatly appreciated.