From 48e5577f2db044b05eece3deab7cc9973a1c0a27 Mon Sep 17 00:00:00 2001 From: Esteban Escobar Date: Tue, 21 Feb 2023 22:21:39 -0500 Subject: [PATCH] gitignore updates --- .gitignore | 2 ++ REVISITS.md | 13 ++++++-- exercises/conversions/as_ref_mut.rs | 15 +++++---- exercises/conversions/from_str.rs | 26 +++++++++++++-- exercises/conversions/try_from_into.rs | 45 ++++++++++++++++++++++++-- 5 files changed, 87 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index c14f9227..3d2b8832 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,5 @@ rust-project.json !.vscode/extensions.json *.iml *.o + +REVISITS.md diff --git a/REVISITS.md b/REVISITS.md index f29c4903..1b2f3b48 100644 --- a/REVISITS.md +++ b/REVISITS.md @@ -34,7 +34,16 @@ return results to new Vec vector 35) All threads exercises :(. handles and join methods for thread completion wait in the main thread. 36) threads2 exercises important for understanding synchorization primitize Mutex 37) threads3 - again - all threads exercises. -38) all macros exercises +38) all macros +39) Conversions from_str be sure to understand map_err() for error projection! +40) All conversions man: + ```let mut x = 5; + let x_ref = &mut x; -conversions + let x_ref2 = x_ref.as_mut(); // convert to mutable reference + + *x_ref2 = 10; // modify the value + + assert_eq!(x, 10); + In this example, x_ref is a mutable reference to x. We can use the as_mut method to convert x_ref to a mutable reference, which we store in x_ref2. We can then use x_ref2 to modify the value of x.``` diff --git a/exercises/conversions/as_ref_mut.rs b/exercises/conversions/as_ref_mut.rs index e6a9d114..70460460 100644 --- a/exercises/conversions/as_ref_mut.rs +++ b/exercises/conversions/as_ref_mut.rs @@ -3,25 +3,26 @@ // and https://doc.rust-lang.org/std/convert/trait.AsMut.html, respectively. // Execute `rustlings hint as_ref_mut` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - // 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 { - arg.as_ref().as_bytes().len() +fn byte_counter>(arg: T) -> usize { + arg.as_ref().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) { +fn num_sq(arg: &mut T) +where + T: AsMut, +{ // TODO: Implement the function body. - ??? + *arg.as_mut() *= *arg.as_mut(); } #[cfg(test)] diff --git a/exercises/conversions/from_str.rs b/exercises/conversions/from_str.rs index fe168159..d435b1a6 100644 --- a/exercises/conversions/from_str.rs +++ b/exercises/conversions/from_str.rs @@ -28,8 +28,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 @@ -46,8 +44,30 @@ enum ParsePersonError { impl FromStr for Person { type Err = ParsePersonError; fn from_str(s: &str) -> Result { + if s.is_empty() { + return Err(ParsePersonError::Empty); + } else { + let mycollection: Vec<&str> = s.split(",").collect(); + + if mycollection[0].is_empty() { + return Err(ParsePersonError::NoName); + } else if mycollection.len() != 2 { + return Err(ParsePersonError::BadLen); + } else { + let myage = mycollection[1].parse::().map_err(ParsePersonError::ParseInt)?; + let myname = mycollection[0].to_string(); + + + Ok(Person{ + age: myage, + name: myname + }) + } + } + + } + } -} fn main() { let p = "Mark,20".parse::().unwrap(); diff --git a/exercises/conversions/try_from_into.rs b/exercises/conversions/try_from_into.rs index fa98bc90..3122bbb8 100644 --- a/exercises/conversions/try_from_into.rs +++ b/exercises/conversions/try_from_into.rs @@ -23,8 +23,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, @@ -38,6 +36,23 @@ enum IntoColorError { impl TryFrom<(i16, i16, i16)> for Color { type Error = IntoColorError; fn try_from(tuple: (i16, i16, i16)) -> Result { + + + + if tuple.0 < 0 || tuple.0 > 255 || + tuple.1 < 0 || tuple.1 > 255 || + tuple.2 < 0 || tuple.2 > 255 { + return Err(IntoColorError::IntConversion); + } else { + Ok(Color{ + red: tuple.0 as u8, + green: tuple.1 as u8, + blue: tuple.2 as u8 + }) + } + + + } } @@ -45,6 +60,18 @@ 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[0] < 0 || arr[0] > 255 || + arr[1] < 0 || arr[1] > 255 || + arr[2] < 0 || arr[1] > 255 { + return Err(IntoColorError::IntConversion); + } else { + Ok(Color{ + red:arr[0] as u8, + green:arr[1] as u8, + blue: arr[2] as u8 + }) + } } } @@ -52,6 +79,20 @@ 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); + } else if slice[0] < 0 || slice[0] > 255 || + slice[1] < 0 || slice[1] > 255 || + slice[2] < 0 || slice[2] > 255 { + return Err(IntoColorError::IntConversion) + } else { + Ok(Color{ + red: slice[0] as u8, + green: slice[1] as u8, + blue: slice[2] as u8 + }) + } } }