Skip to content

Commit 15ebce8

Browse files
04102005
1 parent bcdce4a commit 15ebce8

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

exercises/conversions/from_into.rs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,34 @@ impl Default for Person {
4040
// If while parsing the age, something goes wrong, then return the default of
4141
// Person Otherwise, then return an instantiated Person object with the results
4242

43-
// I AM NOT DONE
44-
4543
impl From<&str> for Person {
46-
fn from(s: &str) -> Person {}
44+
fn from(s: &str) -> Person {
45+
if s.is_empty() {
46+
return Person::default();
47+
}
48+
49+
let parts: Vec<&str> = s.split(',').collect();
50+
if parts.len() != 2 {
51+
return Person::default();
52+
}
53+
54+
let name = parts[0].trim();
55+
let age_str = parts[1].trim();
56+
57+
if name.is_empty() {
58+
return Person::default();
59+
}
60+
61+
let age = match age_str.parse::<usize>() {
62+
Ok(age) => age,
63+
Err(_) => return Person::default(),
64+
};
65+
66+
Person {
67+
name: name.to_string(),
68+
age,
69+
}
70+
}
4771
}
4872

4973
fn main() {

0 commit comments

Comments
 (0)