Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,862 questions

51,784 answers

573 users

How to create an infinite loop that runs with a delay in Rust

1 Answer

0 votes
use std::thread::sleep;
use std::time::{Duration, Instant};

fn main() {

    let interval = Duration::from_secs(1);
    let mut next_time = Instant::now() + interval;
    loop {
        println!("{:?}", next_time); 
        sleep(next_time - Instant::now());
        next_time += interval;
    }
}

   
   
/*
run:

Instant { tv_sec: 874827, tv_nsec: 359199172 }
Instant { tv_sec: 874828, tv_nsec: 359199172 }
Instant { tv_sec: 874829, tv_nsec: 359199172 }
Instant { tv_sec: 874830, tv_nsec: 359199172 }
Instant { tv_sec: 874831, tv_nsec: 359199172 }
Instant { tv_sec: 874832, tv_nsec: 359199172 }
Instant { tv_sec: 874833, tv_nsec: 359199172 }
Instant { tv_sec: 874834, tv_nsec: 359199172 }
Instant { tv_sec: 874835, tv_nsec: 359199172 }
Instant { tv_sec: 874836, tv_nsec: 359199172 }
Instant { tv_sec: 874837, tv_nsec: 359199172 }
Instant { tv_sec: 874838, tv_nsec: 359199172 }
Instant { tv_sec: 874839, tv_nsec: 359199172 }
Instant { tv_sec: 874840, tv_nsec: 359199172 }
Instant { tv_sec: 874841, tv_nsec: 359199172 }
Instant { tv_sec: 874842, tv_nsec: 359199172 }
Instant { tv_sec: 874843, tv_nsec: 359199172 }
Instant { tv_sec: 874844, tv_nsec: 359199172 }
Instant { tv_sec: 874845, tv_nsec: 359199172 }
Instant { tv_sec: 874846, tv_nsec: 359199172 }
Instant { tv_sec: 874847, tv_nsec: 359199172 }
Instant { tv_sec: 874848, tv_nsec: 359199172 }
Instant { tv_sec: 874849, tv_nsec: 359199172 }
Instant { tv_sec: 874850, tv_nsec: 359199172 }
Instant { tv_sec: 874851, tv_nsec: 359199172 }
Instant { tv_sec: 874852, tv_nsec: 359199172 }
Instant { tv_sec: 874853, tv_nsec: 359199172 }
Instant { tv_sec: 874854, tv_nsec: 359199172 }
Instant { tv_sec: 874855, tv_nsec: 359199172 }
Instant { tv_sec: 874856, tv_nsec: 359199172 }
Instant { tv_sec: 874857, tv_nsec: 359199172 }
Instant { tv_sec: 874858, tv_nsec: 359199172 }
Instant { tv_sec: 874859, tv_nsec: 359199172 }
Instant { tv_sec: 874860, tv_nsec: 359199172 }
Instant { tv_sec: 874861, tv_nsec: 359199172 }
Instant { tv_sec: 874862, tv_nsec: 359199172 }
Instant { tv_sec: 874863, tv_nsec: 359199172 }
Instant { tv_sec: 874864, tv_nsec: 359199172 }
Instant { tv_sec: 874865, tv_nsec: 359199172 }
Instant { tv_sec: 874866, tv_nsec: 359199172 }
Instant { tv_sec: 874867, tv_nsec: 359199172 }
Instant { tv_sec: 874868, tv_nsec: 359199172 }

*/

 



answered Aug 7, 2024 by avibootz

Related questions

1 answer 87 views
1 answer 102 views
102 views asked May 4, 2023 by avibootz
1 answer 267 views
267 views asked May 27, 2021 by avibootz
5 answers 253 views
253 views asked Jul 30, 2023 by avibootz
1 answer 140 views
140 views asked May 31, 2021 by avibootz
1 answer 162 views
162 views asked Dec 22, 2020 by avibootz
2 answers 179 views
...