From a513d90cff96ecd7cefeb171e1e4bc788f44406c Mon Sep 17 00:00:00 2001 From: lanzy Date: Sat, 23 Mar 2024 19:12:35 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A7=20temp=20save?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- exercises/19_smart_pointers/cow1.rs | 2 -- exercises/20_threads/threads1.rs | 2 +- exercises/20_threads/threads2.rs | 12 +++++---- exercises/20_threads/threads3.rs | 9 +++---- exercises/21_macros/macros1.rs | 3 +-- exercises/21_macros/macros2.rs | 8 +++--- exercises/21_macros/macros3.rs | 2 +- exercises/21_macros/macros4.rs | 3 +-- exercises/22_clippy/Cargo.lock | 7 +++++ exercises/22_clippy/Cargo.toml | 7 +++++ exercises/22_clippy/clippy1.rs | 4 +-- exercises/22_clippy/clippy2.rs | 3 +-- exercises/22_clippy/clippy3.rs | 18 +++++++------ exercises/23_conversions/as_ref_mut.rs | 16 +++++++++--- exercises/23_conversions/from_into.rs | 21 ++++++++++++--- exercises/23_conversions/from_str.rs | 18 +++++++++++-- exercises/23_conversions/try_from_into.rs | 32 ++++++++++++++++++++--- exercises/23_conversions/using_as.rs | 3 +-- 18 files changed, 119 insertions(+), 51 deletions(-) create mode 100644 exercises/22_clippy/Cargo.lock create mode 100644 exercises/22_clippy/Cargo.toml diff --git a/exercises/19_smart_pointers/cow1.rs b/exercises/19_smart_pointers/cow1.rs index 3cbdcf73..c1606ea3 100644 --- a/exercises/19_smart_pointers/cow1.rs +++ b/exercises/19_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/20_threads/threads1.rs b/exercises/20_threads/threads1.rs index 80b6def3..5dbfb9c7 100644 --- a/exercises/20_threads/threads1.rs +++ b/exercises/20_threads/threads1.rs @@ -8,7 +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 +26,7 @@ fn main() { let mut results: Vec = 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 { diff --git a/exercises/20_threads/threads2.rs b/exercises/20_threads/threads2.rs index 62dad80d..e4e6dc86 100644 --- a/exercises/20_threads/threads2.rs +++ b/exercises/20_threads/threads2.rs @@ -7,9 +7,9 @@ // Execute `rustlings hint threads2` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE -use std::sync::Arc; +use std::borrow::BorrowMut; +use std::sync::{Arc, Mutex}; use std::thread; use std::time::Duration; @@ -18,14 +18,16 @@ 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; + // status_shared.borrow_mut().jobs_completed += 1; + // status_shared.jobs_completed += 1; }); handles.push(handle); } @@ -34,6 +36,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); } } diff --git a/exercises/20_threads/threads3.rs b/exercises/20_threads/threads3.rs index acb97b4b..446754fe 100644 --- a/exercises/20_threads/threads3.rs +++ b/exercises/20_threads/threads3.rs @@ -3,10 +3,8 @@ // 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; use std::time::Duration; @@ -27,18 +25,19 @@ impl Queue { } fn send_tx(q: Queue, tx: mpsc::Sender) -> () { + let tx2 = tx.clone(); thread::spawn(move || { for val in q.first_half { println!("sending {:?}", val); - tx.send(val).unwrap(); + tx.clone().send(val).unwrap(); thread::sleep(Duration::from_secs(1)); } }); - thread::spawn(move || { + thread::spawn( move|| { for val in q.second_half { println!("sending {:?}", val); - tx.send(val).unwrap(); + tx2.clone().send(val).unwrap(); thread::sleep(Duration::from_secs(1)); } }); diff --git a/exercises/21_macros/macros1.rs b/exercises/21_macros/macros1.rs index 678de6ee..87319302 100644 --- a/exercises/21_macros/macros1.rs +++ b/exercises/21_macros/macros1.rs @@ -3,7 +3,6 @@ // Execute `rustlings hint macros1` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE macro_rules! my_macro { () => { @@ -12,5 +11,5 @@ macro_rules! my_macro { } fn main() { - my_macro(); + my_macro!(); } diff --git a/exercises/21_macros/macros2.rs b/exercises/21_macros/macros2.rs index 788fc16a..b3d9f713 100644 --- a/exercises/21_macros/macros2.rs +++ b/exercises/21_macros/macros2.rs @@ -3,14 +3,14 @@ // 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!(); +} diff --git a/exercises/21_macros/macros3.rs b/exercises/21_macros/macros3.rs index b795c149..345b46a7 100644 --- a/exercises/21_macros/macros3.rs +++ b/exercises/21_macros/macros3.rs @@ -5,9 +5,9 @@ // Execute `rustlings hint macros3` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE mod macros { + #[macro_export] macro_rules! my_macro { () => { println!("Check out my macro!"); diff --git a/exercises/21_macros/macros4.rs b/exercises/21_macros/macros4.rs index 71b45a09..d0f4b47c 100644 --- a/exercises/21_macros/macros4.rs +++ b/exercises/21_macros/macros4.rs @@ -3,13 +3,12 @@ // 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/22_clippy/Cargo.lock b/exercises/22_clippy/Cargo.lock new file mode 100644 index 00000000..d0fc406e --- /dev/null +++ b/exercises/22_clippy/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "clippy3" +version = "0.0.1" diff --git a/exercises/22_clippy/Cargo.toml b/exercises/22_clippy/Cargo.toml new file mode 100644 index 00000000..44dc91f8 --- /dev/null +++ b/exercises/22_clippy/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "clippy3" +version = "0.0.1" +edition = "2021" +[[bin]] +name = "clippy3" +path = "clippy3.rs" \ No newline at end of file diff --git a/exercises/22_clippy/clippy1.rs b/exercises/22_clippy/clippy1.rs index e0c6ce7c4..1c468d38 100644 --- a/exercises/22_clippy/clippy1.rs +++ b/exercises/22_clippy/clippy1.rs @@ -9,12 +9,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/22_clippy/clippy2.rs b/exercises/22_clippy/clippy2.rs index 9b87a0b7..dd149aee 100644 --- a/exercises/22_clippy/clippy2.rs +++ b/exercises/22_clippy/clippy2.rs @@ -3,12 +3,11 @@ // 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 { + if let Some(x) = option{ res += x; } println!("{}", res); diff --git a/exercises/22_clippy/clippy3.rs b/exercises/22_clippy/clippy3.rs index 5a95f5b8..a3147f69 100644 --- a/exercises/22_clippy/clippy3.rs +++ b/exercises/22_clippy/clippy3.rs @@ -3,28 +3,30 @@ // 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(); + if my_option.is_some() { + println!("There's something!"); + } else { + println!("There's nothing!"); } 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); + vec![1, 2, 3, 4, 5].resize(0, 5); + println!("This Vec is empty, see? {:?}", ()); 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); + // value_a = value_b; + // value_b = value_a; println!("value a: {}; value b: {}", value_a, value_b); } diff --git a/exercises/23_conversions/as_ref_mut.rs b/exercises/23_conversions/as_ref_mut.rs index 2ba9e3f0..5eb8cd6f 100644 --- a/exercises/23_conversions/as_ref_mut.rs +++ b/exercises/23_conversions/as_ref_mut.rs @@ -9,23 +9,31 @@ // 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 +where T: AsRef +{ 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 +where T: AsRef +{ arg.as_ref().chars().count() } // Squares a number using as_mut(). // TODO: Add the appropriate trait bound. -fn num_sq(arg: &mut T) { +fn num_sq(arg: &mut T) +where T: for<'a> AsMut<&'a mut Box> + for<'a> Mul<&'a mut T, Output = T>// + std::ops::MulAssign<&mut Box> +{ // TODO: Implement the function body. - ??? + *arg = (*arg) * arg; } #[cfg(test)] diff --git a/exercises/23_conversions/from_into.rs b/exercises/23_conversions/from_into.rs index 11787c37..0b847ed6 100644 --- a/exercises/23_conversions/from_into.rs +++ b/exercises/23_conversions/from_into.rs @@ -24,7 +24,6 @@ impl Default for Person { } } - // Your task is to complete this implementation in order for the line `let p1 = // Person::from("Mark,20")` to compile. Please note that you'll need to parse the // age component into a `usize` with something like `"4".parse::()`. The @@ -41,10 +40,24 @@ 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 {} + fn from(s: &str) -> Person { + if s.len() == 0 { + return Person::default(); + } + let fields = s.split(',').collect::>(); + if fields.len() != 2 { + return Person::default(); + } + let name = fields[0].trim().to_string(); + if name.len() == 0 { + return Person::default(); + } + match fields[1].trim().parse::() { + Err(_) => Person::default(), + Ok(age) => Person { name, age }, + } + } } fn main() { diff --git a/exercises/23_conversions/from_str.rs b/exercises/23_conversions/from_str.rs index 34472c32..83f248a2 100644 --- a/exercises/23_conversions/from_str.rs +++ b/exercises/23_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 fields = s.split(',').collect::>(); + if fields.len() != 2 { + return Err(ParsePersonError::BadLen); + } + let name = fields[0].trim().to_string(); + if name.len() == 0 { + return Err(ParsePersonError::NoName); + } + let age = fields[1].trim().parse::().map_err(ParsePersonError::ParseInt)?; + Ok(Person{ + name, + age, + }) } } diff --git a/exercises/23_conversions/try_from_into.rs b/exercises/23_conversions/try_from_into.rs index 32d6ef39..d87a6fe4 100644 --- a/exercises/23_conversions/try_from_into.rs +++ b/exercises/23_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. @@ -37,17 +35,35 @@ enum IntoColorError { // 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 convert_to_u8 (value: T) -> Result + where T: TryInto +{ + Ok(value.try_into().or(Err(IntoColorError::IntConversion))?) +} + // Tuple implementation impl TryFrom<(i16, i16, i16)> for Color { type Error = IntoColorError; fn try_from(tuple: (i16, i16, i16)) -> Result { + Ok(Color { + red: convert_to_u8(tuple.0)?, + green: convert_to_u8(tuple.1)?, + blue: convert_to_u8(tuple.2)?, + }) } } // Array implementation -impl TryFrom<[i16; 3]> for Color { +impl TryFrom<[T; 3]> for Color +where T: TryInto + Copy +{ type Error = IntoColorError; - fn try_from(arr: [i16; 3]) -> Result { + fn try_from(arr: [T; 3]) -> Result { + Ok(Color { + red: convert_to_u8(arr[0])?, + green: convert_to_u8(arr[1])?, + blue: convert_to_u8(arr[2])?, + }) } } @@ -55,6 +71,14 @@ 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); + } + Ok(Color { + red: convert_to_u8(slice[0])?, + green: convert_to_u8(slice[1])?, + blue: convert_to_u8(slice[2])?, + }) } } diff --git a/exercises/23_conversions/using_as.rs b/exercises/23_conversions/using_as.rs index 414cef3a..54c90730 100644 --- a/exercises/23_conversions/using_as.rs +++ b/exercises/23_conversions/using_as.rs @@ -10,11 +10,10 @@ // 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() {