How to Put the Current Thread to Sleep in Rust

Created
Modified

Using sleep_ms Method

The thread::sleep_ms() method puts the current thread to sleep for at least the specified amount of time. For example,

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

fn main() {
  println!("{:?}", SystemTime::now());

  // Sleep
  thread::sleep(Duration::from_millis(4000));
  println!("{:?}", SystemTime::now());
}
SystemTime { tv_sec: 1653360055, tv_nsec: 949217277 }
SystemTime { tv_sec: 1653360059, tv_nsec: 949449668 }

Related Tags