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.
// 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"),
}
}
}

View File

@ -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<u128> = 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 {

View File

@ -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);
}
}

View File

@ -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<u32>) -> () {
let qc = Arc::new(q);
let qc1 = Arc::clone(&qc);
let qc2 = Arc::clone(&qc);
fn send_tx(q: Arc<Queue>, tx: mpsc::Sender<u32>) {
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)
}