Threads, macros and quiz4 exercises complete.

This commit is contained in:
Emre AYDIN 2021-01-11 18:05:25 +03:00
parent 0c9f55747b
commit 84e88274fb
6 changed files with 22 additions and 17 deletions

View file

@ -1,8 +1,6 @@
// macros1.rs // macros1.rs
// Make me compile! Execute `rustlings hint macros1` for hints :) // Make me compile! Execute `rustlings hint macros1` for hints :)
// I AM NOT DONE
macro_rules! my_macro { macro_rules! my_macro {
() => { () => {
println!("Check out my macro!"); println!("Check out my macro!");
@ -10,5 +8,5 @@ macro_rules! my_macro {
} }
fn main() { fn main() {
my_macro(); my_macro!();
} }

View file

@ -1,12 +1,11 @@
// macros2.rs // macros2.rs
// Make me compile! Execute `rustlings hint macros2` for hints :) // Make me compile! Execute `rustlings hint macros2` for hints :)
// I AM NOT DONE
fn main() { fn main() {
my_macro!(); my_macro!();
} }
#[macro_export]
macro_rules! my_macro { macro_rules! my_macro {
() => { () => {
println!("Check out my macro!"); println!("Check out my macro!");

View file

@ -2,8 +2,7 @@
// Make me compile, without taking the macro out of the module! // Make me compile, without taking the macro out of the module!
// Execute `rustlings hint macros3` for hints :) // Execute `rustlings hint macros3` for hints :)
// I AM NOT DONE #[macro_use]
mod macros { mod macros {
macro_rules! my_macro { macro_rules! my_macro {
() => { () => {

View file

@ -1,18 +1,17 @@
// macros4.rs // macros4.rs
// Make me compile! Execute `rustlings hint macros4` for hints :) // Make me compile! Execute `rustlings hint macros4` for hints :)
// I AM NOT DONE
macro_rules! my_macro { macro_rules! my_macro {
() => { () => {
println!("Check out my macro!"); println!("Check out my macro!");
} };
($val:expr) => { ($val:expr) => {
println!("Look at this other macro: {}", $val); println!("Look at this other macro: {}", $val);
} };
} }
fn main() { fn main() {
my_macro!(); my_macro!();
my_macro!(7777); my_macro!(7777);
my_macro!("abc");
} }

View file

@ -6,6 +6,17 @@
// Write a macro that passes the quiz! No hints this time, you can do it! // Write a macro that passes the quiz! No hints this time, you can do it!
// I AM NOT DONE // I AM NOT DONE
#[macro_use]
mod macros {
macro_rules! my_macro {
("world!") => {
"Hello world!"
};
("goodbye!") => {
"Hello goodbye!"
};
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {

View file

@ -6,9 +6,7 @@
// of "waiting..." and the program ends without timing out when running, // of "waiting..." and the program ends without timing out when running,
// you've got it :) // you've got it :)
// I AM NOT DONE use std::sync::{Arc, Mutex};
use std::sync::Arc;
use std::thread; use std::thread;
use std::time::Duration; use std::time::Duration;
@ -17,16 +15,17 @@ struct JobStatus {
} }
fn main() { fn main() {
let status = Arc::new(JobStatus { jobs_completed: 0 }); let status = Arc::new(Mutex::new(JobStatus { jobs_completed: 0 }));
let status_shared = status.clone(); let status_shared = status.clone();
thread::spawn(move || { thread::spawn(move || {
for _ in 0..10 { for _ in 0..10 {
thread::sleep(Duration::from_millis(250)); thread::sleep(Duration::from_millis(250));
status_shared.jobs_completed += 1; status_shared.lock().unwrap().jobs_completed += 1;
} }
}); });
while status.jobs_completed < 10 { while status.lock().unwrap().jobs_completed < 10 {
println!("waiting... "); println!("waiting... ");
thread::sleep(Duration::from_millis(500)); thread::sleep(Duration::from_millis(500));
} }
println!("{}", status.lock().unwrap().jobs_completed);
} }