Tickers in Rust
Tickers are used when you want to do something repeatedly at regular intervals. Here’s an example of a ticker that ticks periodically until we stop it.
use std::time::{Duration, Instant};
use std::thread;
use std::sync::mpsc;
fn main() {
// Create a channel to send and receive messages
let (tx, rx) = mpsc::channel();
// Spawn a new thread to handle the ticker
let handle = thread::spawn(move || {
let mut ticker = Instant::now();
let period = Duration::from_millis(500);
loop {
match rx.try_recv() {
Ok(_) => return, // Exit the loop if a message is received
Err(mpsc::TryRecvError::Empty) => {
// Check if it's time for the next tick
if ticker.elapsed() >= period {
println!("Tick at {:?}", Instant::now());
ticker = Instant::now(); // Reset the ticker
}
}
Err(_) => return, // Channel is disconnected, exit the loop
}
thread::sleep(Duration::from_millis(10)); // Small sleep to prevent busy-waiting
}
});
// Let the ticker run for 1600ms
thread::sleep(Duration::from_millis(1600));
// Stop the ticker
tx.send(()).unwrap();
handle.join().unwrap();
println!("Ticker stopped");
}
In this Rust implementation, we use a channel to communicate between the main thread and the ticker thread. The ticker thread runs in a loop, checking if it’s time for the next tick and printing the current time if so.
When we run this program, the ticker should tick 3 times before we stop it.
$ cargo run
Tick at Instant { tv_sec: 1623456789, tv_nsec: 123456789 }
Tick at Instant { tv_sec: 1623456789, tv_nsec: 623456789 }
Tick at Instant { tv_sec: 1623456790, tv_nsec: 123456789 }
Ticker stopped
Note that the actual timestamps will differ when you run the program. The important thing is that you should see three ticks before the ticker is stopped.
This example demonstrates how to create a simple ticker in Rust using standard library features. Unlike Go, Rust doesn’t have built-in ticker functionality, so we’ve implemented it using a combination of Instant
for time tracking, thread::sleep
for introducing delays, and channels for communication between threads.