This commit is contained in:
horaoen 2024-01-30 02:06:42 +08:00
parent f6913fa6e9
commit 2785367132
4 changed files with 29 additions and 30 deletions

View File

@ -12,8 +12,6 @@
// //
// Execute `rustlings hint cow1` or use the `hint` watch subcommand for a hint. // Execute `rustlings hint cow1` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
use std::borrow::Cow; use std::borrow::Cow;
fn abs_all<'a, 'b>(input: &'a mut Cow<'b, [i32]>) -> &'a mut Cow<'b, [i32]> { 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 slice = [0, 1, 2];
let mut input = Cow::from(&slice[..]); let mut input = Cow::from(&slice[..]);
match abs_all(&mut input) { 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 slice = vec![0, 1, 2];
let mut input = Cow::from(slice); let mut input = Cow::from(slice);
match abs_all(&mut input) { 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 slice = vec![-1, 0, 1];
let mut input = Cow::from(slice); let mut input = Cow::from(slice);
match abs_all(&mut input) { match abs_all(&mut input) {
// TODO Cow::Owned(_) => Ok(()),
_ => Err("Expected owned value"),
} }
} }
} }

View File

@ -8,8 +8,6 @@
// Execute `rustlings hint threads1` or use the `hint` watch subcommand for a // Execute `rustlings hint threads1` or use the `hint` watch subcommand for a
// hint. // hint.
// I AM NOT DONE
use std::thread; use std::thread;
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
@ -26,7 +24,7 @@ fn main() {
let mut results: Vec<u128> = vec![]; let mut results: Vec<u128> = vec![];
for handle in handles { 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 { if results.len() != 10 {

View File

@ -7,9 +7,7 @@
// Execute `rustlings hint threads2` or use the `hint` watch subcommand for a // Execute `rustlings hint threads2` or use the `hint` watch subcommand for a
// hint. // hint.
// I AM NOT DONE use std::sync::{Arc, Mutex};
use std::sync::Arc;
use std::thread; use std::thread;
use std::time::Duration; use std::time::Duration;
@ -18,14 +16,14 @@ struct JobStatus {
} }
fn main() { fn main() {
let status = Arc::new(JobStatus { jobs_completed: 0 }); let status = 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 status_shared = Arc::clone(&status);
let handle = thread::spawn(move || { let handle = thread::spawn(move || {
thread::sleep(Duration::from_millis(250)); thread::sleep(Duration::from_millis(250));
// TODO: You must take an action before you update a shared value // 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); handles.push(handle);
} }
@ -34,6 +32,6 @@ fn main() {
// TODO: Print the value of the JobStatus.jobs_completed. Did you notice // 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 // anything interesting in the output? Do you have to 'join' on all the
// handles? // handles?
println!("jobs completed {}", ???); println!("jobs completed {}", status.lock().unwrap().jobs_completed);
} }
} }

View File

@ -1,10 +1,8 @@
// threads3.rs // threads3.rs
//
// Execute `rustlings hint threads3` or use the `hint` watch subcommand for a // Execute `rustlings hint threads3` or use the `hint` watch subcommand for a
//
// hint. // 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;
@ -26,32 +24,36 @@ impl Queue {
} }
} }
fn send_tx(q: Queue, tx: mpsc::Sender<u32>) -> () { fn send_tx(q: Arc<Queue>, tx: mpsc::Sender<u32>) {
let qc = Arc::new(q); let q1 = Arc::clone(&q);
let qc1 = Arc::clone(&qc); let tx1 = tx.clone();
let qc2 = Arc::clone(&qc);
thread::spawn(move || { thread::spawn(move || {
for val in &qc1.first_half { for val in &q1.first_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));
} }
}); })
.join()
.unwrap();
let q2 = Arc::clone(&q);
let tx2 = tx.clone();
thread::spawn(move || { thread::spawn(move || {
for val in &qc2.second_half { for val in &q2.second_half {
println!("sending {:?}", val); println!("sending {:?}", val);
tx.send(*val).unwrap(); tx2.send(*val).unwrap();
thread::sleep(Duration::from_secs(1)); thread::sleep(Duration::from_secs(1));
} }
}); })
.join()
.unwrap();
} }
#[test] #[test]
fn main() { fn main() {
let (tx, rx) = mpsc::channel(); let (tx, rx) = mpsc::channel();
let queue = Queue::new(); let queue = Arc::new(Queue::new());
let queue_length = queue.length; let queue_length = queue.length;
send_tx(queue, tx); send_tx(queue, tx);
@ -63,5 +65,5 @@ fn main() {
} }
println!("total numbers received: {}", total_received); println!("total numbers received: {}", total_received);
assert_eq!(total_received, queue_length)
} }