pub async fn sleep(secs: u64)Expand description
Suspends the current async task for the specified number of seconds.
This convenience function provides a simple interface for second-based delays,
using the same high-precision platform-native timing as Timer::after.
§Arguments
secs- The number of seconds to sleep
§Platform Integration
Uses the same platform-native scheduling as Timer for consistent precision.
§Examples
use native_executor::timer::sleep;
async fn delayed_operation() {
println!("Starting operation");
sleep(2).await; // High-precision 2-second delay
println!("Operation resumed after 2 seconds");
}Examples found in repository?
examples/timers.rs (line 23)
7fn main() {
8 spawn(async {
9 println!("Starting timers example");
10
11 // Use the Timer API
12 println!("Waiting for 500ms...");
13 Timer::after(Duration::from_millis(500)).await;
14 println!("500ms elapsed");
15
16 // Use the seconds convenience method
17 println!("Waiting for 1 second...");
18 Timer::after_secs(1).await;
19 println!("1 second elapsed");
20
21 // Use the sleep convenience function
22 println!("Sleeping for 2 seconds...");
23 sleep(2).await;
24 println!("2 seconds elapsed");
25
26 println!("Timers example completed");
27 })
28 .detach();
29
30 // Keep the main thread alive
31 std::thread::sleep(Duration::from_secs(4));
32}