Setel interval karat
#[allow(dead_code)]
enum Time {
Millisecond(u8),
Second(u8),
Minute(u8),
Hour(u8)
}
#[allow(dead_code)]
fn set_interval(callback: fn() -> (), time: Time) -> () {
let duration = match time {
Time::Millisecond(value) => Duration::from_millis(value as u64),
Time::Second(value) => Duration::from_secs(value as u64),
Time::Minute(value) => Duration::from_secs(value as u64 * 60),
Time::Hour(value) => Duration::from_secs(value as u64 * 60 * 60)
};
callback();
loop {
sleep(duration);
callback()
}
}
fn main() {
set_interval(|| {
println!("Hello World after every 1 second")
}, Time::Second(1))
}
Ahmad Khaefi