How can I put the current thread to sleep?

Rust 1.4+

Duration and sleep have returned and are stable!

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

fn main() {
    thread::sleep(Duration::from_millis(4000));
}

You could also use Duration::from_secs(4), which might be more obvious in this case.

The solution below for 1.0 will continue to work if you prefer it, due to the nature of semantic versioning.

Rust 1.0+

Duration wasn’t made stable in time for 1.0, so there’s a new function in town – thread::sleep_ms:

use std::thread;

fn main() {
    thread::sleep_ms(4000);
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)