From e77301ba3a6228ae0bb791e39148e171e92d5e5f Mon Sep 17 00:00:00 2001 From: finchxx <1470073710@qq.com> Date: Mon, 27 Jan 2025 17:56:26 +0800 Subject: [PATCH] update --- exercises/macros/macros1.rs | 3 +-- exercises/macros/macros2.rs | 11 ++++++----- exercises/macros/macros3.rs | 3 +-- exercises/macros/macros4.rs | 5 ++--- exercises/threads/threads1.rs | 9 +++++++-- exercises/threads/threads2.rs | 13 +++++++------ exercises/threads/threads3.rs | 8 ++++---- 7 files changed, 28 insertions(+), 24 deletions(-) diff --git a/exercises/macros/macros1.rs b/exercises/macros/macros1.rs index 678de6eec..873193025 100644 --- a/exercises/macros/macros1.rs +++ b/exercises/macros/macros1.rs @@ -3,7 +3,6 @@ // Execute `rustlings hint macros1` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE macro_rules! my_macro { () => { @@ -12,5 +11,5 @@ macro_rules! my_macro { } fn main() { - my_macro(); + my_macro!(); } diff --git a/exercises/macros/macros2.rs b/exercises/macros/macros2.rs index 788fc16a9..ad1c68147 100644 --- a/exercises/macros/macros2.rs +++ b/exercises/macros/macros2.rs @@ -3,14 +3,15 @@ // Execute `rustlings hint macros2` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE - -fn main() { - my_macro!(); -} macro_rules! my_macro { () => { println!("Check out my macro!"); }; } + + +fn main() { + my_macro!(); +} + diff --git a/exercises/macros/macros3.rs b/exercises/macros/macros3.rs index b795c1493..e1b872f4d 100644 --- a/exercises/macros/macros3.rs +++ b/exercises/macros/macros3.rs @@ -5,9 +5,8 @@ // Execute `rustlings hint macros3` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE - mod macros { + #[macro_export] macro_rules! my_macro { () => { println!("Check out my macro!"); diff --git a/exercises/macros/macros4.rs b/exercises/macros/macros4.rs index 71b45a095..70a7e9045 100644 --- a/exercises/macros/macros4.rs +++ b/exercises/macros/macros4.rs @@ -3,13 +3,12 @@ // Execute `rustlings hint macros4` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE - #[rustfmt::skip] macro_rules! my_macro { + () => { println!("Check out my macro!"); - } + }; ($val:expr) => { println!("Look at this other macro: {}", $val); } diff --git a/exercises/threads/threads1.rs b/exercises/threads/threads1.rs index 80b6def3e..3f70bfe59 100644 --- a/exercises/threads/threads1.rs +++ b/exercises/threads/threads1.rs @@ -8,8 +8,6 @@ // Execute `rustlings hint threads1` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE - use std::thread; use std::time::{Duration, Instant}; @@ -27,6 +25,13 @@ fn main() { let mut results: Vec = vec![]; for handle in handles { // TODO: a struct is returned from thread::spawn, can you use it? + match handle.join() { + Ok(time) => { + results.push(time); + } + Err(_) => {} + } + } if results.len() != 10 { diff --git a/exercises/threads/threads2.rs b/exercises/threads/threads2.rs index 62dad80d6..eda528034 100644 --- a/exercises/threads/threads2.rs +++ b/exercises/threads/threads2.rs @@ -7,9 +7,8 @@ // Execute `rustlings hint threads2` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE -use std::sync::Arc; +use std::sync::{Arc, Mutex}; use std::thread; use std::time::Duration; @@ -18,22 +17,24 @@ struct JobStatus { } fn main() { - let status = Arc::new(JobStatus { jobs_completed: 0 }); + let status = Arc::new(Mutex::new(JobStatus { jobs_completed: 0 })); let mut handles = vec![]; for _ in 0..10 { let status_shared = Arc::clone(&status); let handle = thread::spawn(move || { thread::sleep(Duration::from_millis(250)); - // TODO: You must take an action before you update a shared value - status_shared.jobs_completed += 1; + let mut status = status_shared.lock().unwrap(); + status.jobs_completed += 1; }); handles.push(handle); } for handle in handles { handle.join().unwrap(); + // let completed = status.lock().unwrap().jobs_completed; // TODO: Print the value of the JobStatus.jobs_completed. Did you notice // anything interesting in the output? Do you have to 'join' on all the // handles? - println!("jobs completed {}", ???); + let mut status_shared = status.lock().unwrap(); + println!("jobs completed {}", status_shared.jobs_completed); } } diff --git a/exercises/threads/threads3.rs b/exercises/threads/threads3.rs index db7d41ba6..f420a8e27 100644 --- a/exercises/threads/threads3.rs +++ b/exercises/threads/threads3.rs @@ -3,8 +3,6 @@ // Execute `rustlings hint threads3` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE - use std::sync::mpsc; use std::sync::Arc; use std::thread; @@ -31,18 +29,20 @@ fn send_tx(q: Queue, tx: mpsc::Sender) -> () { let qc1 = Arc::clone(&qc); let qc2 = Arc::clone(&qc); + let tx1 = tx.clone(); thread::spawn(move || { for val in &qc1.first_half { println!("sending {:?}", val); - tx.send(*val).unwrap(); + tx1.send(*val).unwrap(); thread::sleep(Duration::from_secs(1)); } }); + let tx2= tx.clone(); thread::spawn(move || { for val in &qc2.second_half { println!("sending {:?}", val); - tx.send(*val).unwrap(); + tx2.send(*val).unwrap(); thread::sleep(Duration::from_secs(1)); } });