From cfc401e1c7d442557afe46148bd47c022cc7af11 Mon Sep 17 00:00:00 2001 From: MatyTan <1396568121@qq.com> Date: Sun, 27 Nov 2022 08:28:07 +0800 Subject: [PATCH] day11 --- exercises/macros/macros1.rs | 4 +--- exercises/macros/macros2.rs | 9 +++------ exercises/macros/macros3.rs | 1 + exercises/threads/threads2.rs | 4 ---- exercises/threads/threads3.rs | 6 +++--- 5 files changed, 8 insertions(+), 16 deletions(-) diff --git a/exercises/macros/macros1.rs b/exercises/macros/macros1.rs index 634d0a70..3c0696af 100644 --- a/exercises/macros/macros1.rs +++ b/exercises/macros/macros1.rs @@ -1,8 +1,6 @@ // macros1.rs // Execute `rustlings hint macros1` or use the `hint` watch subcommand for a hint. -// 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 f6092cab..01db557f 100644 --- a/exercises/macros/macros2.rs +++ b/exercises/macros/macros2.rs @@ -1,14 +1,11 @@ // macros2.rs // 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 106f1c6d..a558c02e 100644 --- a/exercises/macros/macros3.rs +++ b/exercises/macros/macros3.rs @@ -4,6 +4,7 @@ // I AM NOT DONE +#[macro_use] mod macros { macro_rules! my_macro { () => { diff --git a/exercises/threads/threads2.rs b/exercises/threads/threads2.rs index 6f39a159..c6dca815 100644 --- a/exercises/threads/threads2.rs +++ b/exercises/threads/threads2.rs @@ -3,8 +3,6 @@ // Building on the last exercise, we want all of the threads to complete their work but this time // the spawned threads need to be in charge of updating a shared value: JobStatus.jobs_completed -// I AM NOT DONE - use std::sync::{Arc, Mutex}; use std::thread; use std::time::Duration; @@ -14,11 +12,9 @@ struct JobStatus { } fn main() { - let status = Arc::new(JobStatus { jobs_completed: 0 }); let mtx = Arc::new(Mutex::new(JobStatus { jobs_completed: 0 })); let mut handles = vec![]; for _ in 0..10 { - let status_shared = Arc::clone(&status); let mtx = Arc::clone(&mtx); let handle = thread::spawn(move || { thread::sleep(Duration::from_millis(250)); diff --git a/exercises/threads/threads3.rs b/exercises/threads/threads3.rs index 9e9f285a..1c66d37a 100644 --- a/exercises/threads/threads3.rs +++ b/exercises/threads/threads3.rs @@ -1,8 +1,6 @@ // threads3.rs // 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; @@ -28,6 +26,7 @@ fn send_tx(q: Queue, tx: mpsc::Sender) -> () { let qc = Arc::new(q); let qc1 = Arc::clone(&qc); let qc2 = Arc::clone(&qc); + let tx1 = tx.clone(); //tx发送者发送到多个协程需要clone thread::spawn(move || { for val in &qc1.first_half { @@ -40,7 +39,7 @@ fn send_tx(q: Queue, tx: mpsc::Sender) -> () { thread::spawn(move || { for val in &qc2.second_half { println!("sending {:?}", val); - tx.send(*val).unwrap(); + tx1.send(*val).unwrap(); thread::sleep(Duration::from_secs(1)); } }); @@ -54,6 +53,7 @@ fn main() { send_tx(queue, tx); let mut total_received: u32 = 0; + //接受者 for received in rx { println!("Got: {}", received); total_received += 1;