This commit is contained in:
Mir Saheb Ali 2024-03-20 13:12:53 +05:30
parent 43ec7fc92b
commit 87c9ea44a0
10 changed files with 32 additions and 32 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]> {
@ -49,6 +47,8 @@ mod tests {
let mut input = Cow::from(&slice[..]); let mut input = Cow::from(&slice[..]);
match abs_all(&mut input) { match abs_all(&mut input) {
// TODO // TODO
Cow::Owned(..) => Err("Mutated"),
Cow::Borrowed(..) => Ok(()),
} }
} }
@ -61,6 +61,8 @@ mod tests {
let mut input = Cow::from(slice); let mut input = Cow::from(slice);
match abs_all(&mut input) { match abs_all(&mut input) {
// TODO // TODO
Cow::Owned(..) => Ok(()),
Cow::Borrowed(..) => Err("Error"),
} }
} }
@ -73,6 +75,8 @@ mod tests {
let mut input = Cow::from(slice); let mut input = Cow::from(slice);
match abs_all(&mut input) { match abs_all(&mut input) {
// TODO // TODO
Cow::Owned(c) => Ok(c.push(3)),
_ => Err("Error"),
} }
} }
} }

View File

@ -1,6 +1,6 @@
// threads1.rs // threads1.rs
// //
// This program spawns multiple threads that each run for at least 250ms, and // INFO: 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 // 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 // wait until all the spawned threads have finished and should collect their
// return values into a vector. // return values into a vector.
@ -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};
@ -27,6 +25,8 @@ 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? // TODO: a struct is returned from thread::spawn, can you use it?
let x = handle.join().unwrap();
results.push(x)
} }
if results.len() != 10 { if results.len() != 10 {

View File

@ -1,30 +1,29 @@
// threads2.rs // threads2.rs
// //
// Building on the last exercise, we want all of the threads to complete their // TASK: 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 // work but this time the spawned threads need to be in charge of updating a
// shared value: JobStatus.jobs_completed // shared value: JobStatus.jobs_completed
// //
// 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;
#[derive(Debug)]
struct JobStatus { struct JobStatus {
jobs_completed: u32, jobs_completed: u32,
} }
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
let mut status_shared = status_shared.lock().unwrap();
status_shared.jobs_completed += 1; status_shared.jobs_completed += 1;
}); });
handles.push(handle); handles.push(handle);
@ -34,6 +33,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());
} }
} }

View File

@ -3,8 +3,6 @@
// 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;
@ -28,21 +26,27 @@ impl Queue {
fn send_tx(q: Queue, tx: mpsc::Sender<u32>) -> () { fn send_tx(q: Queue, tx: mpsc::Sender<u32>) -> () {
let qc = Arc::new(q); let qc = Arc::new(q);
let mut t_arc = Arc::new(tx);
let qc1 = Arc::clone(&qc); let qc1 = Arc::clone(&qc);
let qc2 = Arc::clone(&qc); let qc2 = Arc::clone(&qc);
let mut t_arc1 = Arc::clone(&t_arc);
thread::spawn(move || { thread::spawn(move || {
for val in &qc1.first_half { for val in &qc1.first_half {
println!("sending {:?}", val); println!("sending {:?}", val);
tx.send(*val).unwrap(); t_arc1.send(*val).unwrap();
thread::sleep(Duration::from_secs(1)); thread::sleep(Duration::from_secs(1));
} }
}); });
let mut t_arc2 = Arc::clone(&t_arc);
thread::spawn(move || { thread::spawn(move || {
for val in &qc2.second_half { for val in &qc2.second_half {
println!("sending {:?}", val); println!("sending {:?}", val);
tx.send(*val).unwrap();
t_arc2.clone().send(*val).unwrap();
thread::sleep(Duration::from_secs(1)); thread::sleep(Duration::from_secs(1));
} }
}); });

View File

@ -3,8 +3,6 @@
// Execute `rustlings hint macros1` or use the `hint` watch subcommand for a // Execute `rustlings hint macros1` or use the `hint` watch subcommand for a
// hint. // hint.
// I AM NOT DONE
macro_rules! my_macro { macro_rules! my_macro {
() => { () => {
println!("Check out my macro!"); println!("Check out my macro!");
@ -12,5 +10,5 @@ macro_rules! my_macro {
} }
fn main() { fn main() {
my_macro(); my_macro!();
} }

View File

@ -3,14 +3,12 @@
// Execute `rustlings hint macros2` or use the `hint` watch subcommand for a // Execute `rustlings hint macros2` or use the `hint` watch subcommand for a
// hint. // hint.
// I AM NOT DONE
fn main() {
my_macro!();
}
macro_rules! my_macro { macro_rules! my_macro {
() => { () => {
println!("Check out my macro!"); println!("Check out my macro!");
}; };
} }
fn main() {
my_macro!();
}

View File

@ -5,8 +5,7 @@
// Execute `rustlings hint macros3` or use the `hint` watch subcommand for a // Execute `rustlings hint macros3` or use the `hint` watch subcommand for a
// hint. // hint.
// I AM NOT DONE #[macro_use]
mod macros { mod macros {
macro_rules! my_macro { macro_rules! my_macro {
() => { () => {

View File

@ -3,14 +3,12 @@
// Execute `rustlings hint macros4` or use the `hint` watch subcommand for a // Execute `rustlings hint macros4` or use the `hint` watch subcommand for a
// hint. // hint.
// I AM NOT DONE
#[rustfmt::skip] #[rustfmt::skip]
macro_rules! my_macro { macro_rules! my_macro {
() => { () => {
println!("Check out my macro!"); println!("Check out my macro!");
} };
($val:expr) => { ( $val:expr) => {
println!("Look at this other macro: {}", $val); println!("Look at this other macro: {}", $val);
} }
} }

View File

@ -14,7 +14,7 @@
use std::f32; use std::f32;
fn main() { fn main() {
let pi = 3.14f32; let pi = f32::consts::PI;
let radius = 5.00f32; let radius = 5.00f32;
let area = pi * f32::powi(radius, 2); let area = pi * f32::powi(radius, 2);

0
temp_36580_ThreadId1 Normal file
View File