File tree Expand file tree Collapse file tree 1 file changed +27
-3
lines changed Expand file tree Collapse file tree 1 file changed +27
-3
lines changed Original file line number Diff line number Diff line change @@ -40,10 +40,34 @@ impl Default for Person {
40
40
// If while parsing the age, something goes wrong, then return the default of
41
41
// Person Otherwise, then return an instantiated Person object with the results
42
42
43
- // I AM NOT DONE
44
-
45
43
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
+ }
47
71
}
48
72
49
73
fn main ( ) {
You can’t perform that action at this time.
0 commit comments