Created
January 24, 2022 17:31
-
-
Save Rickasaurus/03a61139ab63fc188d36d8b59916cdf1 to your computer and use it in GitHub Desktop.
from_into.rs solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Steps: | |
// 1. If the length of the provided string is 0, then return the default of Person | |
// 2. Split the given string on the commas present in it | |
// 3. Extract the first element from the split operation and use it as the name | |
// 4. If the name is empty, then return the default of Person | |
// 5. Extract the other element from the split operation and parse it into a `usize` as the age | |
// 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.len() == 0 { | |
Person::default() | |
} else { | |
let splitStr: Vec<&str> = s.split(",").collect(); | |
match &splitStr[..] { | |
[nameStr, ageStr] => | |
match ageStr.parse::<usize>() { | |
Ok (age) => | |
if nameStr != &"" { | |
Person { | |
name: nameStr.to_string(), | |
age: age, | |
} | |
} | |
else { | |
Person::default() | |
} | |
, | |
Err (_) => Person::default() | |
} | |
_ => Person::default(), | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment