Programming Tips - rust: Run some code after a delay

Date: 2025jul18 Language: rust Q. rust: Run some code after a delay A. Here is a nice way that does it without any dependencies. It creates a new thread which sleeps for the duration you want, runs your code, then the thread exits.
use std::thread; use std::time::Duration; fn main() { thread::spawn(|| { thread::sleep(Duration::from_secs(5)); myfunction(); // Will be run after 5 seconds }); }