This commit is contained in:
MatyTan 2022-11-27 08:28:07 +08:00
parent 012359ddfc
commit cfc401e1c7
5 changed files with 8 additions and 16 deletions

View File

@ -1,8 +1,6 @@
// macros1.rs // macros1.rs
// Execute `rustlings hint macros1` or use the `hint` watch subcommand for a hint. // Execute `rustlings hint macros1` or use the `hint` watch subcommand for a hint.
// 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,14 +1,11 @@
// macros2.rs // macros2.rs
// Execute `rustlings hint macros2` or use the `hint` watch subcommand for a hint. // 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 { macro_rules! my_macro {
() => { () => {
println!("Check out my macro!"); println!("Check out my macro!");
}; };
} }
fn main() {
my_macro!();
}

View File

@ -4,6 +4,7 @@
// I AM NOT DONE // I AM NOT DONE
#[macro_use]
mod macros { mod macros {
macro_rules! my_macro { macro_rules! my_macro {
() => { () => {

View File

@ -3,8 +3,6 @@
// Building on the last exercise, we want all of the threads to complete their work but this time // 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 // 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::sync::{Arc, Mutex};
use std::thread; use std::thread;
use std::time::Duration; use std::time::Duration;
@ -14,11 +12,9 @@ struct JobStatus {
} }
fn main() { fn main() {
let status = Arc::new(JobStatus { jobs_completed: 0 });
let mtx = Arc::new(Mutex::new(JobStatus { jobs_completed: 0 })); let mtx = Arc::new(Mutex::new(JobStatus { jobs_completed: 0 }));
let mut handles = vec![]; let mut handles = vec![];
for _ in 0..10 { for _ in 0..10 {
let status_shared = Arc::clone(&status);
let mtx = Arc::clone(&mtx); let mtx = Arc::clone(&mtx);
let handle = thread::spawn(move || { let handle = thread::spawn(move || {
thread::sleep(Duration::from_millis(250)); thread::sleep(Duration::from_millis(250));

View File

@ -1,8 +1,6 @@
// threads3.rs // threads3.rs
// Execute `rustlings hint threads3` or use the `hint` watch subcommand for a hint. // Execute `rustlings hint threads3` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
use std::sync::mpsc; use std::sync::mpsc;
use std::sync::Arc; use std::sync::Arc;
use std::thread; use std::thread;
@ -28,6 +26,7 @@ fn send_tx(q: Queue, tx: mpsc::Sender<u32>) -> () {
let qc = Arc::new(q); let qc = Arc::new(q);
let qc1 = Arc::clone(&qc); let qc1 = Arc::clone(&qc);
let qc2 = Arc::clone(&qc); let qc2 = Arc::clone(&qc);
let tx1 = tx.clone(); //tx发送者发送到多个协程需要clone
thread::spawn(move || { thread::spawn(move || {
for val in &qc1.first_half { for val in &qc1.first_half {
@ -40,7 +39,7 @@ fn send_tx(q: Queue, tx: mpsc::Sender<u32>) -> () {
thread::spawn(move || { thread::spawn(move || {
for val in &qc2.second_half { for val in &qc2.second_half {
println!("sending {:?}", val); println!("sending {:?}", val);
tx.send(*val).unwrap(); tx1.send(*val).unwrap();
thread::sleep(Duration::from_secs(1)); thread::sleep(Duration::from_secs(1));
} }
}); });
@ -54,6 +53,7 @@ fn main() {
send_tx(queue, tx); send_tx(queue, tx);
let mut total_received: u32 = 0; let mut total_received: u32 = 0;
//接受者
for received in rx { for received in rx {
println!("Got: {}", received); println!("Got: {}", received);
total_received += 1; total_received += 1;