From 84e88274fbaf7be04b16c376b0e3784c77e8bb59 Mon Sep 17 00:00:00 2001 From: Emre AYDIN <aeaydin1@gmail.com> Date: Mon, 11 Jan 2021 18:05:25 +0300 Subject: [PATCH] Threads, macros and quiz4 exercises complete. --- exercises/macros/macros1.rs | 4 +--- exercises/macros/macros2.rs | 3 +-- exercises/macros/macros3.rs | 3 +-- exercises/macros/macros4.rs | 7 +++---- exercises/quiz4.rs | 11 +++++++++++ exercises/threads/threads1.rs | 11 +++++------ 6 files changed, 22 insertions(+), 17 deletions(-) diff --git a/exercises/macros/macros1.rs b/exercises/macros/macros1.rs index ed0dac8..de2584a 100644 --- a/exercises/macros/macros1.rs +++ b/exercises/macros/macros1.rs @@ -1,8 +1,6 @@ // macros1.rs // Make me compile! Execute `rustlings hint macros1` for hints :) -// I AM NOT DONE - macro_rules! my_macro { () => { println!("Check out my macro!"); @@ -10,5 +8,5 @@ macro_rules! my_macro { } fn main() { - my_macro(); + my_macro!(); } diff --git a/exercises/macros/macros2.rs b/exercises/macros/macros2.rs index d0be123..6583dde 100644 --- a/exercises/macros/macros2.rs +++ b/exercises/macros/macros2.rs @@ -1,12 +1,11 @@ // macros2.rs // Make me compile! Execute `rustlings hint macros2` for hints :) -// I AM NOT DONE - fn main() { my_macro!(); } +#[macro_export] macro_rules! my_macro { () => { println!("Check out my macro!"); diff --git a/exercises/macros/macros3.rs b/exercises/macros/macros3.rs index 93a4311..868f3dd 100644 --- a/exercises/macros/macros3.rs +++ b/exercises/macros/macros3.rs @@ -2,8 +2,7 @@ // Make me compile, without taking the macro out of the module! // Execute `rustlings hint macros3` for hints :) -// I AM NOT DONE - +#[macro_use] mod macros { macro_rules! my_macro { () => { diff --git a/exercises/macros/macros4.rs b/exercises/macros/macros4.rs index 3a74807..a8eea00 100644 --- a/exercises/macros/macros4.rs +++ b/exercises/macros/macros4.rs @@ -1,18 +1,17 @@ // macros4.rs // Make me compile! Execute `rustlings hint macros4` for hints :) -// I AM NOT DONE - macro_rules! my_macro { () => { println!("Check out my macro!"); - } + }; ($val:expr) => { println!("Look at this other macro: {}", $val); - } + }; } fn main() { my_macro!(); my_macro!(7777); + my_macro!("abc"); } diff --git a/exercises/quiz4.rs b/exercises/quiz4.rs index 6c47480..8bcfb4d 100644 --- a/exercises/quiz4.rs +++ b/exercises/quiz4.rs @@ -6,6 +6,17 @@ // Write a macro that passes the quiz! No hints this time, you can do it! // I AM NOT DONE +#[macro_use] +mod macros { + macro_rules! my_macro { + ("world!") => { + "Hello world!" + }; + ("goodbye!") => { + "Hello goodbye!" + }; + } +} #[cfg(test)] mod tests { diff --git a/exercises/threads/threads1.rs b/exercises/threads/threads1.rs index b475b4a..c92df83 100644 --- a/exercises/threads/threads1.rs +++ b/exercises/threads/threads1.rs @@ -6,9 +6,7 @@ // of "waiting..." and the program ends without timing out when running, // you've got it :) -// I AM NOT DONE - -use std::sync::Arc; +use std::sync::{Arc, Mutex}; use std::thread; use std::time::Duration; @@ -17,16 +15,17 @@ struct JobStatus { } 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(); thread::spawn(move || { for _ in 0..10 { 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... "); thread::sleep(Duration::from_millis(500)); } + println!("{}", status.lock().unwrap().jobs_completed); }