1- // The From trait is used for value-to-value conversions.
2- // If From is implemented correctly for a type, the Into trait should work conversely.
3- // You can read more about it at https://doc.rust-lang.org/std/convert/trait.From.html
4- // Execute `rustlings hint from_into` or use the `hint` watch subcommand for a hint.
1+ // from_into.rs
2+ //
3+ // The From trait is used for value-to-value conversions. If From is implemented
4+ // correctly for a type, the Into trait should work conversely. You can read
5+ // more about it at https://doc.rust-lang.org/std/convert/trait.From.html
6+ //
7+ // Execute `rustlings hint from_into` or use the `hint` watch subcommand for a
8+ // hint.
59
610#[ derive( Debug ) ]
711struct Person {
@@ -20,20 +24,21 @@ impl Default for Person {
2024 }
2125}
2226
23- // Your task is to complete this implementation
24- // in order for the line `let p = Person::from("Mark,20")` to compile
25- // Please note that you'll need to parse the age component into a `usize`
26- // with something like `"4".parse::<usize>()`. The outcome of this needs to
27- // be handled appropriately.
27+ // Your task is to complete this implementation in order for the line `let p =
28+ // Person::from("Mark,20")` to compile Please note that you'll need to parse the
29+ // age component into a `usize` with something like `"4".parse::<usize>()`. The
30+ // outcome of this needs to be handled appropriately.
2831//
2932// Steps:
30- // 1. If the length of the provided string is 0, then return the default of Person
31- // 2. Split the given string on the commas present in it
32- // 3. Extract the first element from the split operation and use it as the name
33- // 4. If the name is empty, then return the default of Person
34- // 5. Extract the other element from the split operation and parse it into a `usize` as the age
35- // If while parsing the age, something goes wrong, then return the default of Person
36- // Otherwise, then return an instantiated Person object with the results
33+ // 1. If the length of the provided string is 0, then return the default of
34+ // Person.
35+ // 2. Split the given string on the commas present in it.
36+ // 3. Extract the first element from the split operation and use it as the name.
37+ // 4. If the name is empty, then return the default of Person.
38+ // 5. Extract the other element from the split operation and parse it into a
39+ // `usize` as the age.
40+ // If while parsing the age, something goes wrong, then return the default of
41+ // Person Otherwise, then return an instantiated Person object with the results
3742
3843// I AM NOT DONE
3944
@@ -77,7 +82,8 @@ mod tests {
7782 }
7883 #[ test]
7984 fn test_bad_age ( ) {
80- // Test that "Mark,twenty" will return the default person due to an error in parsing age
85+ // Test that "Mark,twenty" will return the default person due to an
86+ // error in parsing age
8187 let p = Person :: from ( "Mark,twenty" ) ;
8288 assert_eq ! ( p. name, "John" ) ;
8389 assert_eq ! ( p. age, 30 ) ;
0 commit comments