diff --git a/exercises/conversions/as_ref_mut.rs b/exercises/conversions/as_ref_mut.rs index e6a9d11..1557f32 100644 --- a/exercises/conversions/as_ref_mut.rs +++ b/exercises/conversions/as_ref_mut.rs @@ -3,25 +3,19 @@ // 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 { +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) { + *arg.as_mut() = *arg.as_mut() * *arg.as_mut(); } #[cfg(test)] diff --git a/exercises/conversions/from_into.rs b/exercises/conversions/from_into.rs index 6c272c3..fd8a3d3 100644 --- a/exercises/conversions/from_into.rs +++ b/exercises/conversions/from_into.rs @@ -35,10 +35,16 @@ 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 { + let vec: Vec<&str> = s.split(",").collect(); + let name = vec[0].trim(); + let age = vec[vec.len() - 1].parse::(); + if vec.len() == 2 && name.len() > 0 && age.is_ok() { + Person { name: name.to_string(), age: age.unwrap() } + } else { + Person::default() + } } } diff --git a/exercises/conversions/from_str.rs b/exercises/conversions/from_str.rs index fe16815..f3a64d5 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,22 @@ enum ParsePersonError { impl FromStr for Person { type Err = ParsePersonError; fn from_str(s: &str) -> Result { + let vec: Vec<&str> = s.split(",").collect(); + let name = vec[0].trim(); + let age = vec[vec.len() - 1].parse::(); + if vec.len() == 2 && name.len() > 0 && age.is_ok() { + Ok(Person { name: name.to_string(), age: age.unwrap() }) + } else { + if s.trim().len() == 0 { + Err(ParsePersonError::Empty) + } else if vec.len() != 2 { + Err(ParsePersonError::BadLen) + } else if name.len() == 0 { + Err(ParsePersonError::NoName) + } else { + Err(ParsePersonError::ParseInt(age.unwrap_err())) + } + } } } diff --git a/exercises/conversions/try_from_into.rs b/exercises/conversions/try_from_into.rs index fa98bc9..c0a5125 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, @@ -34,10 +32,30 @@ enum IntoColorError { // 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 into_color(collection: Vec) -> Result { + let mut color = Color {red: 0, green: 0, blue: 0}; + if collection.len() < 3 { return Err(IntoColorError::BadLen) } + for (i, num) in collection.iter().enumerate() { + if *num >= 0 && *num <= 255 { + let val = *num as u8; + match i { + 0 => color.red = val, + 1 => color.green = val, + 2 => color.blue = val, + _ => return Err(IntoColorError::BadLen), + } + } else { + return Err(IntoColorError::IntConversion); + } + } + Ok(color) +} + // Tuple implementation impl TryFrom<(i16, i16, i16)> for Color { type Error = IntoColorError; fn try_from(tuple: (i16, i16, i16)) -> Result { + into_color(vec![tuple.0, tuple.1, tuple.2]) } } @@ -45,6 +63,7 @@ impl TryFrom<(i16, i16, i16)> for Color { impl TryFrom<[i16; 3]> for Color { type Error = IntoColorError; fn try_from(arr: [i16; 3]) -> Result { + into_color(arr.to_vec()) } } @@ -52,6 +71,7 @@ impl TryFrom<[i16; 3]> for Color { impl TryFrom<&[i16]> for Color { type Error = IntoColorError; fn try_from(slice: &[i16]) -> Result { + into_color(slice.to_vec()) } } diff --git a/exercises/conversions/using_as.rs b/exercises/conversions/using_as.rs index 8c9b711..0d961ca 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() {