What I would like to achieve using the Rust language is to execute a closure or method after X seconds (without waiting for it). Being more used to languages like C#, a simple solution would be to spawn a thread, sleep for X seconds, and execute whatever needs to be done.
I tried this:
fn fire_and_forget(&self) {
thread::spawn(|| {
sleep(Duration::from_secs(10));
self.do_something();
});
}
Obviously, this doesn't work. Depending on my context, I get errors like error[E0277]: cannot be sent between threads safely or error[E0759]: self has an anonymous lifetime '_ but it needs to satisfy a 'static lifetime requirement.
I hope there is solution for this. This "context" I am referring to is quite specific: this fire_and_forget method will be called from the outside so I am not sure I can change its signature with generic lifetimes.
Any idea if there is a solution / workaround / unsafe way for this?