How to slow down a loop

I’ve been googling for a while and cannot find a very basic solution to the following problem. Let say I want my loop to slow down. Usually I would call a sleep(3) (or a version of it in other programming languages ) how do I do it in Rust:


fn main(){

    loop{
        println!("sleeping for 3  sec ");
        sleep(3);  // what is the correct function call here
    }

}


use std::{thread, time};

fn main() {
    let delay = time::Duration::from_secs(3);

    loop{
        println!("sleeping for 3  sec ");
        thread::sleep(delay);
    }
}

See: https://doc.rust-lang.org/std/thread/fn.sleep.html

3 Likes