diff --git a/exercises/19_smart_pointers/cow1.rs b/exercises/19_smart_pointers/cow1.rs index fcd3e0bb..5816e5c9 100644 --- a/exercises/19_smart_pointers/cow1.rs +++ b/exercises/19_smart_pointers/cow1.rs @@ -12,8 +12,6 @@ // // Execute `rustlings hint cow1` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - use std::borrow::Cow; fn abs_all<'a, 'b>(input: &'a mut Cow<'b, [i32]>) -> &'a mut Cow<'b, [i32]> { @@ -48,7 +46,8 @@ mod tests { let slice = [0, 1, 2]; let mut input = Cow::from(&slice[..]); match abs_all(&mut input) { - // TODO + Cow::Borrowed(_) => Ok(()), + _ => Err("Expected borrowed value"), } } @@ -60,7 +59,8 @@ mod tests { let slice = vec![0, 1, 2]; let mut input = Cow::from(slice); match abs_all(&mut input) { - // TODO + Cow::Owned(_) => Ok(()), + _ => Err("Expected owned value"), } } @@ -72,7 +72,8 @@ mod tests { let slice = vec![-1, 0, 1]; let mut input = Cow::from(slice); match abs_all(&mut input) { - // TODO + Cow::Owned(_) => Ok(()), + _ => Err("Expected owned value"), } } } diff --git a/exercises/20_threads/threads1.rs b/exercises/20_threads/threads1.rs index 80b6def3..a4c89854 100644 --- a/exercises/20_threads/threads1.rs +++ b/exercises/20_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}; @@ -26,7 +24,7 @@ fn main() { let mut results: Vec = vec![]; for handle in handles { - // TODO: a struct is returned from thread::spawn, can you use it? + results.push(handle.join().unwrap()); } if results.len() != 10 { diff --git a/exercises/20_threads/threads2.rs b/exercises/20_threads/threads2.rs index 62dad80d..ec5c61b2 100644 --- a/exercises/20_threads/threads2.rs +++ b/exercises/20_threads/threads2.rs @@ -7,9 +7,7 @@ // 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,14 +16,14 @@ 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; + status_shared.lock().unwrap().jobs_completed += 1; }); handles.push(handle); } @@ -34,6 +32,6 @@ fn main() { // 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 {}", ???); + println!("jobs completed {}", status.lock().unwrap().jobs_completed); } } diff --git a/exercises/20_threads/threads3.rs b/exercises/20_threads/threads3.rs index 91006bbc..66af420a 100644 --- a/exercises/20_threads/threads3.rs +++ b/exercises/20_threads/threads3.rs @@ -1,10 +1,8 @@ // 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; @@ -26,32 +24,36 @@ impl Queue { } } -fn send_tx(q: Queue, tx: mpsc::Sender) -> () { - let qc = Arc::new(q); - let qc1 = Arc::clone(&qc); - let qc2 = Arc::clone(&qc); - +fn send_tx(q: Arc, tx: mpsc::Sender) { + let q1 = Arc::clone(&q); + let tx1 = tx.clone(); thread::spawn(move || { - for val in &qc1.first_half { + for val in &q1.first_half { println!("sending {:?}", val); - tx.send(*val).unwrap(); + tx1.send(*val).unwrap(); thread::sleep(Duration::from_secs(1)); } - }); + }) + .join() + .unwrap(); + let q2 = Arc::clone(&q); + let tx2 = tx.clone(); thread::spawn(move || { - for val in &qc2.second_half { + for val in &q2.second_half { println!("sending {:?}", val); - tx.send(*val).unwrap(); + tx2.send(*val).unwrap(); thread::sleep(Duration::from_secs(1)); } - }); + }) + .join() + .unwrap(); } #[test] fn main() { let (tx, rx) = mpsc::channel(); - let queue = Queue::new(); + let queue = Arc::new(Queue::new()); let queue_length = queue.length; send_tx(queue, tx); @@ -63,5 +65,5 @@ fn main() { } println!("total numbers received: {}", total_received); - assert_eq!(total_received, queue_length) } +