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
- How to get time from milliseconds in Rust
- What type to use for datetime in Rust
- How to add second to time in Rust
- How to get execution time in Rust
- Using now to get current time in Rust
- How to get current date in Rust
- How to format datetime in Rust
- How to format time in Rust
- How to convert timestamp to datetime in Rust
More of Rust
- How to match whitespace with a regex in Rust?
- How to match a URL with a regex in Rust?
- How to perform matrix operations in Rust?
- How to replace strings using Rust regex?
- How to use a tuple as a key in a Rust HashMap?
- Regex example to match multiline string in Rust?
- How to use regex lookahead in Rust?
- How to use regex to match a double quote in Rust?
- How to sort the keys in a Rust HashMap?
- How to use non-capturing groups in Rust regex?
See more codes...