diff --git a/exercises/conversions/from_into.rs b/exercises/conversions/from_into.rs index f5a76d0..6df8dd2 100644 --- a/exercises/conversions/from_into.rs +++ b/exercises/conversions/from_into.rs @@ -33,21 +33,23 @@ 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 person: Vec<&str> = s.split(",").collect(); - match person.len() { - 2 => match person[0].to_owned().len() { - 0 => Err(String::from("Error")), - _ => Ok(Person { - name: person[0].to_owned(), - age: person[1].parse::<usize>().unwrap(), - }), - }, - _ => Err(String::from("Error")), + if person.len() != 2 { + Person::default() + } else { + let name = person[0].to_owned(); + if name.len() == 0 { + Person::default() + } else { + let age = person[1].parse::<usize>(); + match age { + Ok(a) => Person { name, age: a }, + _ => Person::default(), + } + } } } }