rustHow to sleep in Rust
Sleeping in Rust is done using the std::thread::sleep function. This function takes a Duration as an argument, which is a type representing a span of time.
use std::thread::sleep;
use std::time::Duration;
// Sleep for 2 seconds
sleep(Duration::from_secs(2));
The code above will sleep for 2 seconds.
Code explanation
use std::thread::sleep: imports thesleepfunction from thestd::threadmodule.use std::time::Duration: imports theDurationtype from thestd::timemodule.Duration::from_secs(2): creates aDurationrepresenting 2 seconds.sleep(Duration::from_secs(2)): calls thesleepfunction with theDurationrepresenting 2 seconds.
Helpful links
Related
- Using now to get current time in Rust
- How to get time from milliseconds in Rust
- How to get current date in Rust
- How to get execution time in Rust
- How to convert datetime to timestamp in Rust
- How to format datetime in Rust
- How to add second to time in Rust
- How to convert timestamp to datetime in Rust
- What type to use for datetime in Rust
More of Rust
- How to map a Rust slice?
- How to match a URL with a regex in Rust?
- How to use regex to match a double quote in Rust?
- How to ignore case in Rust regex?
- How to make regex case insensitive in Rust?
- How to replace a capture group using Rust regex?
- How to perform matrix operations in Rust?
- How to use binary regex in Rust?
- How to use regex to match a group in Rust?
- How to create a HashMap of structs in Rust?
See more codes...