From ef0e19b5c17747e47bd9fd11e46f007a0c0870a3 Mon Sep 17 00:00:00 2001 From: Nisarga P Date: Fri, 2 Feb 2024 00:08:59 -0800 Subject: [PATCH] added --- .vscode/extensions.json | 5 ++++ exercises/clippy/clippy1.rs | 5 +--- exercises/clippy/clippy2.rs | 4 +-- exercises/clippy/clippy3.rs | 13 +++------- exercises/conversions/as_ref_mut.rs | 14 +++++------ exercises/conversions/from_into.rs | 19 ++++++++++++-- exercises/conversions/from_str.rs | 18 +++++++++++-- exercises/conversions/try_from_into.rs | 35 ++++++++++++++++++++++++-- exercises/conversions/using_as.rs | 4 +-- exercises/macros/macros1.rs | 4 +-- exercises/macros/macros2.rs | 10 +++----- exercises/macros/macros3.rs | 3 +-- exercises/macros/macros4.rs | 4 +-- exercises/smart_pointers/cow1.rs | 2 -- exercises/threads/threads1.rs | 5 ++-- exercises/threads/threads2.rs | 14 +++++------ exercises/threads/threads3.rs | 11 ++++---- 17 files changed, 107 insertions(+), 63 deletions(-) create mode 100644 .vscode/extensions.json diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 00000000..b85de749 --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,5 @@ +{ + "recommendations": [ + "rust-lang.rust-analyzer" + ] +} diff --git a/exercises/clippy/clippy1.rs b/exercises/clippy/clippy1.rs index 95c0141f..7996b372 100644 --- a/exercises/clippy/clippy1.rs +++ b/exercises/clippy/clippy1.rs @@ -8,13 +8,10 @@ // // Execute `rustlings hint clippy1` or use the `hint` watch subcommand for a // hint. - -// I AM NOT DONE - use std::f32; fn main() { - let pi = 3.14f32; + let pi = f32::consts::PI; let radius = 5.00f32; let area = pi * f32::powi(radius, 2); diff --git a/exercises/clippy/clippy2.rs b/exercises/clippy/clippy2.rs index 9b87a0b7..268d3b39 100644 --- a/exercises/clippy/clippy2.rs +++ b/exercises/clippy/clippy2.rs @@ -3,12 +3,10 @@ // Execute `rustlings hint clippy2` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE - fn main() { let mut res = 42; let option = Some(12); - for x in option { + while let Some(x) = option { res += x; } println!("{}", res); diff --git a/exercises/clippy/clippy3.rs b/exercises/clippy/clippy3.rs index 5a95f5b8..51f73f80 100644 --- a/exercises/clippy/clippy3.rs +++ b/exercises/clippy/clippy3.rs @@ -3,28 +3,21 @@ // Here's a couple more easy Clippy fixes, so you can see its utility. // No hints. -// I AM NOT DONE - #[allow(unused_variables, unused_assignments)] fn main() { let my_option: Option<()> = None; - if my_option.is_none() { - my_option.unwrap(); - } let my_arr = &[ - -1, -2, -3 + -1, -2, -3, -4, -5, -6 ]; println!("My array! Here it is: {:?}", my_arr); - let my_empty_vec = vec![1, 2, 3, 4, 5].resize(0, 5); - println!("This Vec is empty, see? {:?}", my_empty_vec); + println!("This Vec is empty, see? {:?}", vec![1,2,3,4,5].resize(0, 0)); let mut value_a = 45; let mut value_b = 66; // Let's swap these two! - value_a = value_b; - value_b = value_a; + std::mem::swap(&mut value_a, &mut value_b); println!("value a: {}; value b: {}", value_a, value_b); } diff --git a/exercises/conversions/as_ref_mut.rs b/exercises/conversions/as_ref_mut.rs index 2ba9e3f0..44689ab3 100644 --- a/exercises/conversions/as_ref_mut.rs +++ b/exercises/conversions/as_ref_mut.rs @@ -7,25 +7,25 @@ // Execute `rustlings hint as_ref_mut` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE +use std::ops::Mul; -// Obtain the number of bytes (not characters) in the given argument. // TODO: Add the AsRef trait appropriately as a trait bound. -fn byte_counter(arg: T) -> usize { +fn byte_counter>(arg: T) -> usize { arg.as_ref().as_bytes().len() } // Obtain the number of characters (not bytes) in the given argument. // TODO: Add the AsRef trait appropriately as a trait bound. -fn char_counter(arg: T) -> usize { +fn char_counter>(arg: T) -> usize { arg.as_ref().chars().count() } // Squares a number using as_mut(). // TODO: Add the appropriate trait bound. -fn num_sq(arg: &mut T) { - // TODO: Implement the function body. - ??? +fn num_sq(arg: &mut T) where T: AsMut { + // TODO: Implementrhs the function body. + let x = arg.as_mut(); + *x = x.mul(*x); } #[cfg(test)] diff --git a/exercises/conversions/from_into.rs b/exercises/conversions/from_into.rs index 60911f3e..3f42a842 100644 --- a/exercises/conversions/from_into.rs +++ b/exercises/conversions/from_into.rs @@ -40,10 +40,25 @@ impl Default for Person { // If while parsing the age, something goes wrong, then return the default of // Person Otherwise, then return an instantiated Person object with the results -// I AM NOT DONE - impl From<&str> for Person { fn from(s: &str) -> Person { + if s.len() == 0 { + return Person::default(); + }; + let split_s = s.split(","); + let split_vec: Vec<&str> = split_s.collect(); + if split_vec.len() < 2 { + return Person::default(); + }; + let name = split_vec[0].to_string(); + if name.len() == 0 { + return Person::default(); + }; + let age: Result = split_vec[1].parse(); + match age { + Ok(a) => Person { name, age: a }, + Err(a) => Person::default() + } } } diff --git a/exercises/conversions/from_str.rs b/exercises/conversions/from_str.rs index 34472c32..21c1b13a 100644 --- a/exercises/conversions/from_str.rs +++ b/exercises/conversions/from_str.rs @@ -31,8 +31,6 @@ enum ParsePersonError { ParseInt(ParseIntError), } -// I AM NOT DONE - // Steps: // 1. If the length of the provided string is 0, an error should be returned // 2. Split the given string on the commas present in it @@ -52,6 +50,22 @@ enum ParsePersonError { impl FromStr for Person { type Err = ParsePersonError; fn from_str(s: &str) -> Result { + if s.len() == 0 { + return Err(ParsePersonError::Empty); + }; + let split_vec: Vec<&str> = s.split(",").collect(); + if split_vec.len() != 2 { + return Err(ParsePersonError::BadLen); + }; + let name = split_vec[0]; + if name.len() == 0 { + return Err(ParsePersonError::NoName); + } + let age: Result = split_vec[1].parse(); + match age { + Ok(a) => Ok(Person{name: name.to_string(), age: a}), + Err(x) => Err(ParsePersonError::ParseInt(x)) + } } } diff --git a/exercises/conversions/try_from_into.rs b/exercises/conversions/try_from_into.rs index 32d6ef39..710f0453 100644 --- a/exercises/conversions/try_from_into.rs +++ b/exercises/conversions/try_from_into.rs @@ -27,8 +27,6 @@ enum IntoColorError { IntConversion, } -// I AM NOT DONE - // Your task is to complete this implementation and return an Ok result of inner // type Color. You need to create an implementation for a tuple of three // integers, an array of three integers, and a slice of integers. @@ -36,11 +34,22 @@ enum IntoColorError { // Note that the implementation for tuple and array will be checked at compile // time, but the slice implementation needs to check the slice length! Also note // that correct RGB color values must be integers in the 0..=255 range. +fn valid_num(num: i16) -> bool { + num >= 0 && num <= 255 +} // Tuple implementation impl TryFrom<(i16, i16, i16)> for Color { type Error = IntoColorError; fn try_from(tuple: (i16, i16, i16)) -> Result { + if !(valid_num(tuple.0) && valid_num(tuple.1) && valid_num(tuple.2)) { + return Err(IntoColorError::IntConversion); + } + return Ok(Color{ + red: tuple.0 as u8, + green: tuple.1 as u8, + blue: tuple.2 as u8 + }); } } @@ -48,6 +57,17 @@ impl TryFrom<(i16, i16, i16)> for Color { impl TryFrom<[i16; 3]> for Color { type Error = IntoColorError; fn try_from(arr: [i16; 3]) -> Result { + if arr.len() != 3 { + return Err(IntoColorError::BadLen); + } + if !(valid_num(arr[0]) && valid_num(arr[1]) && valid_num(arr[2])) { + return Err(IntoColorError::IntConversion); + } + return Ok(Color{ + red: arr[0] as u8, + green: arr[1] as u8, + blue: arr[2] as u8 + }); } } @@ -55,6 +75,17 @@ impl TryFrom<[i16; 3]> for Color { impl TryFrom<&[i16]> for Color { type Error = IntoColorError; fn try_from(slice: &[i16]) -> Result { + if slice.len() != 3 { + return Err(IntoColorError::BadLen); + } + if !(valid_num(slice[0]) && valid_num(slice[1]) && valid_num(slice[2])) { + return Err(IntoColorError::IntConversion); + } + return Ok(Color{ + red: slice[0] as u8, + green: slice[1] as u8, + blue: slice[2] as u8 + }); } } diff --git a/exercises/conversions/using_as.rs b/exercises/conversions/using_as.rs index 414cef3a..a9f1e449 100644 --- a/exercises/conversions/using_as.rs +++ b/exercises/conversions/using_as.rs @@ -10,11 +10,9 @@ // Execute `rustlings hint using_as` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE - fn average(values: &[f64]) -> f64 { let total = values.iter().sum::(); - total / values.len() + total / values.len() as f64 } fn main() { diff --git a/exercises/macros/macros1.rs b/exercises/macros/macros1.rs index 678de6ee..9d0edee3 100644 --- a/exercises/macros/macros1.rs +++ b/exercises/macros/macros1.rs @@ -3,8 +3,6 @@ // Execute `rustlings hint macros1` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE - macro_rules! my_macro { () => { println!("Check out my macro!"); @@ -12,5 +10,5 @@ macro_rules! my_macro { } fn main() { - my_macro(); + my_macro!(); } diff --git a/exercises/macros/macros2.rs b/exercises/macros/macros2.rs index 788fc16a..36736089 100644 --- a/exercises/macros/macros2.rs +++ b/exercises/macros/macros2.rs @@ -3,14 +3,12 @@ // Execute `rustlings hint macros2` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE - -fn main() { - my_macro!(); -} - macro_rules! my_macro { () => { println!("Check out my macro!"); }; } + +fn main() { + my_macro!(); +} \ No newline at end of file diff --git a/exercises/macros/macros3.rs b/exercises/macros/macros3.rs index b795c149..b5be9c39 100644 --- a/exercises/macros/macros3.rs +++ b/exercises/macros/macros3.rs @@ -5,8 +5,7 @@ // Execute `rustlings hint macros3` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE - +#[macro_use] mod macros { macro_rules! my_macro { () => { diff --git a/exercises/macros/macros4.rs b/exercises/macros/macros4.rs index 71b45a09..45d80235 100644 --- a/exercises/macros/macros4.rs +++ b/exercises/macros/macros4.rs @@ -3,13 +3,11 @@ // Execute `rustlings hint macros4` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE - #[rustfmt::skip] macro_rules! my_macro { () => { println!("Check out my macro!"); - } + }; ($val:expr) => { println!("Look at this other macro: {}", $val); } diff --git a/exercises/smart_pointers/cow1.rs b/exercises/smart_pointers/cow1.rs index 486cf986..d1417b41 100644 --- a/exercises/smart_pointers/cow1.rs +++ b/exercises/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]> { diff --git a/exercises/threads/threads1.rs b/exercises/threads/threads1.rs index 80b6def3..9b5953a8 100644 --- a/exercises/threads/threads1.rs +++ b/exercises/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}; @@ -27,6 +25,9 @@ fn main() { let mut results: Vec = vec![]; for handle in handles { // TODO: a struct is returned from thread::spawn, can you use it? + if let Ok(x) = handle.join() { + results.push(x); + } } if results.len() != 10 { diff --git a/exercises/threads/threads2.rs b/exercises/threads/threads2.rs index 62dad80d..d5a3e646 100644 --- a/exercises/threads/threads2.rs +++ b/exercises/threads/threads2.rs @@ -7,9 +7,8 @@ // Execute `rustlings hint threads2` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE - use std::sync::Arc; +use std::sync::Mutex; use std::thread; use std::time::Duration; @@ -18,22 +17,23 @@ 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 status_shared: Arc> = 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; + let mut job_status = status_shared.lock().unwrap(); + job_status.jobs_completed += 1; + }); handles.push(handle); } for handle in handles { - handle.join().unwrap(); // 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/threads/threads3.rs b/exercises/threads/threads3.rs index 91006bbc..02982ec5 100644 --- a/exercises/threads/threads3.rs +++ b/exercises/threads/threads3.rs @@ -3,8 +3,6 @@ // 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; @@ -31,10 +29,13 @@ fn send_tx(q: Queue, tx: mpsc::Sender) -> () { let qc1 = Arc::clone(&qc); let qc2 = Arc::clone(&qc); + let tx1 = tx.clone(); + let tx2 = tx.clone(); + thread::spawn(move || { - for val in &qc1.first_half { + for val in &qc1.first_half.clone() { println!("sending {:?}", val); - tx.send(*val).unwrap(); + tx1.send(*val).unwrap(); thread::sleep(Duration::from_secs(1)); } }); @@ -42,7 +43,7 @@ fn send_tx(q: Queue, tx: mpsc::Sender) -> () { thread::spawn(move || { for val in &qc2.second_half { println!("sending {:?}", val); - tx.send(*val).unwrap(); + tx2.send(*val).unwrap(); thread::sleep(Duration::from_secs(1)); } });