mirror of
https://git.aramjonghu.dev/AramJonghu/rustlings.git
synced 2026-09-05 09:43:31 +02:00
Clean up and unify exercises
This commit is contained in:
@@ -1,12 +1,7 @@
|
||||
// threads1.rs
|
||||
//
|
||||
// This program spawns multiple threads that each run for at least 250ms, and
|
||||
// each thread returns how much time they took to complete. The program should
|
||||
// wait until all the spawned threads have finished and should collect their
|
||||
// return values into a vector.
|
||||
//
|
||||
// Execute `rustlings hint threads1` or use the `hint` watch subcommand for a
|
||||
// hint.
|
||||
|
||||
use std::thread;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
@@ -1,11 +1,6 @@
|
||||
// threads2.rs
|
||||
//
|
||||
// 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
|
||||
//
|
||||
// Execute `rustlings hint threads2` or use the `hint` watch subcommand for a
|
||||
// hint.
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::thread;
|
||||
|
||||
@@ -1,8 +1,3 @@
|
||||
// threads3.rs
|
||||
//
|
||||
// Execute `rustlings hint threads3` or use the `hint` watch subcommand for a
|
||||
// hint.
|
||||
|
||||
use std::sync::mpsc;
|
||||
use std::sync::Arc;
|
||||
use std::thread;
|
||||
@@ -42,20 +37,29 @@ fn send_tx(q: Queue, tx: mpsc::Sender<u32>) -> () {
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn main() {
|
||||
let (tx, rx) = mpsc::channel();
|
||||
let queue = Queue::new();
|
||||
let queue_length = queue.length;
|
||||
|
||||
send_tx(queue, tx);
|
||||
|
||||
let mut total_received: u32 = 0;
|
||||
for received in rx {
|
||||
println!("Got: {}", received);
|
||||
total_received += 1;
|
||||
}
|
||||
|
||||
println!("total numbers received: {}", total_received);
|
||||
assert_eq!(total_received, queue_length)
|
||||
// You can optionally experiment here.
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn threads3() {
|
||||
let (tx, rx) = mpsc::channel();
|
||||
let queue = Queue::new();
|
||||
let queue_length = queue.length;
|
||||
|
||||
send_tx(queue, tx);
|
||||
|
||||
let mut total_received: u32 = 0;
|
||||
for received in rx {
|
||||
println!("Got: {}", received);
|
||||
total_received += 1;
|
||||
}
|
||||
|
||||
println!("total numbers received: {}", total_received);
|
||||
assert_eq!(total_received, queue_length)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user