diff --git a/exercises/conversions/from_into.rs b/exercises/conversions/from_into.rs index aba471d9..0c82135b 100644 --- a/exercises/conversions/from_into.rs +++ b/exercises/conversions/from_into.rs @@ -40,10 +40,36 @@ 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 - +// Requirements for the conversion: +// - string is not empty +// - I can split in 2 elements +// First element is a NOT empty string +// Second element is a string correctly representing a number impl From<&str> for Person { fn from(s: &str) -> Person { + if s.len() == 0 { + Default::default() + } else { + let strs : Vec<&str> = s.split(',').collect(); + if strs.len() == 2 { + let name = strs[0]; + if name.len() > 0 { + let age = strs[1].parse::(); + match age { + Ok(a) => + Person { + name : name.to_string(), + age : a, + }, + Err(_) => Default::default() + } + } else { + Default::default() + } + } else { + Default::default() + } + } } } diff --git a/exercises/conversions/using_as.rs b/exercises/conversions/using_as.rs index 414cef3a..5d29696c 100644 --- a/exercises/conversions/using_as.rs +++ b/exercises/conversions/using_as.rs @@ -10,11 +10,9 @@ // 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() {