-
-
Save infinityb/58a70d3c43dfc85b10c9ad12afb37f5c to your computer and use it in GitHub Desktop.
my first fizzbuzz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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