diff --git a/exercises/conversions/as_ref_mut.rs b/exercises/conversions/as_ref_mut.rs index 84f4a60..12b1af5 100644 --- a/exercises/conversions/as_ref_mut.rs +++ b/exercises/conversions/as_ref_mut.rs @@ -2,17 +2,15 @@ // Read more about them at https://doc.rust-lang.org/std/convert/trait.AsRef.html // and https://doc.rust-lang.org/std/convert/trait.AsMut.html, respectively. -// 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<T>(arg: T) -> usize { +fn byte_counter<T: AsRef<str>>(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<T>(arg: T) -> usize { +fn char_counter<T: AsRef<str> >(arg: T) -> usize { arg.as_ref().chars().count() } diff --git a/exercises/conversions/from_into.rs b/exercises/conversions/from_into.rs index 9d84174..037aaa1 100644 --- a/exercises/conversions/from_into.rs +++ b/exercises/conversions/from_into.rs @@ -33,10 +33,26 @@ 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 vecs:Vec<&str> = s.split(',').collect(); + if let [name, age] = *vecs { + if name.is_empty() || age.is_empty() { + Person::default() + }else { + if let Ok(age) = age.parse::<usize>() { + Person{ + age, + name: name.to_string(), + } + }else{ + Person::default() + } + + } + } else { + Person::default() + } } } diff --git a/exercises/conversions/from_str.rs b/exercises/conversions/from_str.rs index 4beebac..8d7ab4a 100644 --- a/exercises/conversions/from_str.rs +++ b/exercises/conversions/from_str.rs @@ -11,8 +11,6 @@ struct Person { age: usize, } -// 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 @@ -26,6 +24,19 @@ struct Person { impl FromStr for Person { type Err = Box<dyn error::Error>; fn from_str(s: &str) -> Result<Person, Self::Err> { + let vecs:Vec<&str> = s.split(',').collect(); + if let [name, age] = *vecs { + if name.is_empty() || age.is_empty() { + Err("error".into()) + }else { + Ok(Person{ + name: name.to_string(), + age: age.parse::<usize>()? + }) + } + } else { + Err("error".into()) + } } } diff --git a/exercises/conversions/try_from_into.rs b/exercises/conversions/try_from_into.rs index c0b5d98..92ce4c0 100644 --- a/exercises/conversions/try_from_into.rs +++ b/exercises/conversions/try_from_into.rs @@ -12,8 +12,6 @@ struct Color { blue: u8, } -// 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, @@ -26,19 +24,39 @@ struct Color { // Tuple implementation impl TryFrom<(i16, i16, i16)> for Color { type Error = Box<dyn error::Error>; - fn try_from(tuple: (i16, i16, i16)) -> Result<Self, Self::Error> {} + fn try_from((red, green, blue): (i16, i16, i16)) -> Result<Self, Self::Error> { + Color::try_from([red, green, blue]) + } } // Array implementation impl TryFrom<[i16; 3]> for Color { type Error = Box<dyn error::Error>; - fn try_from(arr: [i16; 3]) -> Result<Self, Self::Error> {} + fn try_from(rgb: [i16; 3]) -> Result<Self, Self::Error> { + let range = 0..=255; + if rgb.iter().all(|v| range.contains(v)) { + let [red, green, blue] = rgb; + Ok(Color{ + red: red as u8, + green: green as u8, + blue: blue as u8 + }) + }else{ + Err("error".into()) + } + } } // Slice implementation impl TryFrom<&[i16]> for Color { type Error = Box<dyn error::Error>; - fn try_from(slice: &[i16]) -> Result<Self, Self::Error> {} + fn try_from(slice: &[i16]) -> Result<Self, Self::Error> { + if let [red, green, blue] = *slice{ + Color::try_from([red, green, blue]) + }else{ + Err("error".into()) + } + } } fn main() { diff --git a/exercises/conversions/using_as.rs b/exercises/conversions/using_as.rs index 821309e..f4a33ab 100644 --- a/exercises/conversions/using_as.rs +++ b/exercises/conversions/using_as.rs @@ -5,11 +5,9 @@ // The goal is to make sure that the division does not fail to compile // and returns the proper type. -// I AM NOT DONE - fn average(values: &[f64]) -> f64 { let total = values.iter().fold(0.0, |a, b| a + b); - total / values.len() + total / values.len() as f64 } fn main() {