rust Pause execution for several seconds
To pause execution for several seconds in Rust, you can use the std::thread::sleep
function from the standard library. This function allows you to specify the duration of the pause in seconds. Here is an example code snippet that demonstrates how to pause execution for 5 seconds:
use std::thread;
use std::time::Duration;
fn main() {
println!("Before sleep");
thread::sleep(Duration::from_secs(5));
println!("After sleep");
}
In this code, we import the necessary modules std::thread
and std::time::Duration
. We then call the thread::sleep
function, passing in a Duration
object created with Duration::from_secs(5)
to specify a 5-second pause. After the pause, the program will continue executing and print "After sleep" to the console.
Please note that the thread::sleep
function can potentially be interrupted, so it's important to handle any potential errors that may occur.