Rust Lang

Rust Sleep Function

In development, threading refers to the ability to create and manage multiple threads of execution within a single program. In Rust, we can use Rust’s standard library and the module named “std::thread” to create and manage the threads in Rust programs.

However, although threading is a common task, we will focus on working with the sleep function from the thread module to learn how to pause the execution of a thread for a specific duration.

What Is the Rust Sleep Function?

The Rust sleep function allows us to pause the execution of a thread for a specified amount of time. This can be useful in various situations such as when you must wait for a resource to become available or add a delay between operations.

In Rust, the sleep function is part of the std::thread module in Rust which provides functionality to work with threads. As stated, we can use this sleep function to pause the current thread execution to allow the other threads to continue running.

Rust Sleep Function

The following shows the method definition for the sleep function:

pub fn sleep(dur: Duration)

 

The sleep function takes a single argument which is “dur” as a value of “Duration” type.

The “Duration” type is defined in the std::time module and denotes a length of time such as a number of seconds, milliseconds, or nanoseconds.

The following example shows how to pause a thread for five seconds using the sleep function:

std::thread;
use std::time::{Duration, Instant};

fn main() {
    let start_time = Instant::now();
    println!("Starting to sleep at {:?}", start_time);
    thread::sleep(Duration::from_secs(5));
    let end_time = Instant::now();
    println!("Finished sleeping at {:?}", end_time);
}

 

In the previous example, we use the “Instant” type from the std::time module to record the start and end times of the sleep. We store the start time in start_time using the Instant::now() function and print it out using println!.

After sleeping for 5 seconds using thread::sleep(Duration::from_secs(5)), we record the end time using Instant::now() and store it in end_time.

The resulting output is as follows:

Finished dev [unoptimized + debuginfo] target(s) in 0.02s
Running `target\debug\sleep.exe`
Starting to sleep at Instant { t: 98665.8295817s }
Finished sleeping at Instant { t: 98670.8306501s }

 

Blocking vs Non-Blocking

It’s important to note that the sleep function is a blocking function. Hence, it pauses the current thread and prevents it from doing other work during sleep.

If you need to perform other tasks while the thread sleeps, you can use other non-blocking functions such as the tokio::time::sleep() function.

Conclusion

The Rust sleep function is a useful tool to pause the execution of a Rust program for a specified amount of time. Using the st::thread and Duration modules, you can easily add delays to your programs and allow the other resources to become available.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list