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

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,684 questions

55,436 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
...