From 3d2a78ec0db5007916c2f607d33e5308b19cc703 Mon Sep 17 00:00:00 2001 From: enforcer007 Date: Sun, 13 Nov 2022 17:18:22 +0530 Subject: [PATCH] conversions --- exercises/conversions/as_ref_mut.rs | 10 ++++----- exercises/conversions/from_into.rs | 29 ++++++++++++++++++++++++-- exercises/conversions/from_str.rs | 20 ++++++++++++++++-- exercises/conversions/try_from_into.rs | 18 ++++++++++++++-- exercises/conversions/using_as.rs | 4 +--- 5 files changed, 66 insertions(+), 15 deletions(-) diff --git a/exercises/conversions/as_ref_mut.rs b/exercises/conversions/as_ref_mut.rs index c9eed7d0..68084c53 100644 --- a/exercises/conversions/as_ref_mut.rs +++ b/exercises/conversions/as_ref_mut.rs @@ -3,24 +3,22 @@ // 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 // 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 // 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(). Add the trait bound as is appropriate and // implement the function body. -fn num_sq(arg: &mut T) { - ??? +fn num_sq>(arg: &mut T) { + *arg.as_mut() *= *arg.as_mut() } #[cfg(test)] diff --git a/exercises/conversions/from_into.rs b/exercises/conversions/from_into.rs index 6c272c3b..8e54aefb 100644 --- a/exercises/conversions/from_into.rs +++ b/exercises/conversions/from_into.rs @@ -35,10 +35,35 @@ 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 == "" { + Person::default() + } else { + s.split(",") + .map(|x| x.into()) + .collect::>() + .into() + } + } +} + +impl From> for Person { + fn from(s: Vec) -> Self { + if s.len() != 2 { + return Person::default(); + } + let number = s[1].parse(); + let age; + if number.is_err() || s[0] == "" { + Person::default() + } else { + age = number.unwrap(); + Person { + name: s[0].clone(), + age: age, + } + } } } diff --git a/exercises/conversions/from_str.rs b/exercises/conversions/from_str.rs index fe168159..a94829c1 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,6 +44,24 @@ enum ParsePersonError { impl FromStr for Person { type Err = ParsePersonError; fn from_str(s: &str) -> Result { + if s == "" { + Err(ParsePersonError::Empty) + } else { + let s = s.split(",").map(|x| x.into()).collect::>(); + if s.len() != 2 { + return Err(ParsePersonError::BadLen); + } else if s[0] == "" { + return Err(ParsePersonError::NoName); + } + let number = s[1].parse(); + if let Err(x) = number { + return Err(ParsePersonError::ParseInt(x)); + } + Ok(Person { + name: s[0].clone(), + age: number.unwrap(), + }) + } } } diff --git a/exercises/conversions/try_from_into.rs b/exercises/conversions/try_from_into.rs index fa98bc90..b339020a 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,16 @@ enum IntoColorError { impl TryFrom<(i16, i16, i16)> for Color { type Error = IntoColorError; fn try_from(tuple: (i16, i16, i16)) -> Result { + for i in [tuple.0, tuple.1, tuple.2] { + if i < 0 || i > 255 { + return Err(IntoColorError::IntConversion); + } + } + Ok(Color { + red: tuple.0 as u8, + green: tuple.1 as u8, + blue: tuple.2 as u8, + }) } } @@ -45,6 +53,7 @@ impl TryFrom<(i16, i16, i16)> for Color { impl TryFrom<[i16; 3]> for Color { type Error = IntoColorError; fn try_from(arr: [i16; 3]) -> Result { + Color::try_from((arr[0], arr[1], arr[2])) } } @@ -52,6 +61,11 @@ impl TryFrom<[i16; 3]> for Color { impl TryFrom<&[i16]> for Color { type Error = IntoColorError; fn try_from(slice: &[i16]) -> Result { + if slice.len() != 3 { + Err(IntoColorError::BadLen) + } else { + Color::try_from((slice[0], slice[1], slice[2])) + } } } diff --git a/exercises/conversions/using_as.rs b/exercises/conversions/using_as.rs index 8c9b7113..0d961ca0 100644 --- a/exercises/conversions/using_as.rs +++ b/exercises/conversions/using_as.rs @@ -6,11 +6,9 @@ // and returns the proper type. // 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() {