mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-11 21:29:18 +00:00
day11
This commit is contained in:
parent
012359ddfc
commit
cfc401e1c7
@ -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!();
|
||||
}
|
||||
|
||||
@ -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!();
|
||||
}
|
||||
|
||||
@ -4,6 +4,7 @@
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
#[macro_use]
|
||||
mod macros {
|
||||
macro_rules! my_macro {
|
||||
() => {
|
||||
|
||||
@ -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));
|
||||
|
||||
@ -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<u32>) -> () {
|
||||
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<u32>) -> () {
|
||||
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;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user