Skip to content

Instantly share code, notes, and snippets.

@infinityb
Forked from gyng/fizzbuzz.rs
Last active September 26, 2020 18:33
Show Gist options
  • Save infinityb/58a70d3c43dfc85b10c9ad12afb37f5c to your computer and use it in GitHub Desktop.
Save infinityb/58a70d3c43dfc85b10c9ad12afb37f5c to your computer and use it in GitHub Desktop.
my first fizzbuzz
use std::io::Write;
use std::thread::{self, sleep, JoinHandle};
use std::time::Duration;
fn flush() {
std::io::stdout().flush().unwrap();
}
fn main() {
let mut threads: Vec<JoinHandle<()>> = Vec::new();
threads.push(thread::spawn(|| {
let mut i: u64 = 0;
loop {
i = i + 1;
print!("\n{}", i);
flush();
sleep(Duration::from_secs(1));
}
}));
threads.push(thread::spawn(|| {
loop {
print!("\rfizzbuzz");
flush();
sleep(Duration::from_secs(15));
}
}));
threads.push(thread::spawn(|| {
loop {
print!("\rbuzz");
flush();
sleep(Duration::from_secs(5));
}
}));
threads.push(thread::spawn(|| {
loop {
print!("\rfizz");
flush();
sleep(Duration::from_secs(3));
}
}));
for thread in threads.into_iter() {
let _ = thread.join();
}
}
proff it works
8PS C:\Users\Cube\Desktop> cargo run
Compiling rust-raytracer v0.1.0 (file:C:\Users\Cube\Desktop)
Running `target\rust-raytracer`
fizzbuzz
1
2
fizz
4
buzz
fizz
7
8
fizz
buzz
11
fizz
13
14
fizzbuzz
16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment