File tree Expand file tree Collapse file tree 4 files changed +10
-0
lines changed Expand file tree Collapse file tree 4 files changed +10
-0
lines changed Original file line number Diff line number Diff line change 1
1
// The From trait is used for value-to-value conversions.
2
2
// If From is implemented correctly for a type, the Into trait should work conversely.
3
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.
5
+
4
6
#[ derive( Debug ) ]
5
7
struct Person {
6
8
name : String ,
Original file line number Diff line number Diff line change 4
4
// Additionally, upon implementing FromStr, you can use the `parse` method
5
5
// on strings to generate an object of the implementor type.
6
6
// You can read more about it at https://doc.rust-lang.org/std/str/trait.FromStr.html
7
+ // Execute `rustlings hint from_str` or use the `hint` watch subcommand for a hint.
8
+
7
9
use std:: num:: ParseIntError ;
8
10
use std:: str:: FromStr ;
9
11
@@ -37,6 +39,9 @@ enum ParsePersonError {
37
39
// with something like `"4".parse::<usize>()`
38
40
// 6. If while extracting the name and the age something goes wrong, an error should be returned
39
41
// If everything goes well, then return a Result of a Person object
42
+ //
43
+ // As an aside: `Box<dyn Error>` implements `From<&'_ str>`. This means that if you want to return a
44
+ // string error message, you can do so via just using return `Err("my error message".into())`.
40
45
41
46
impl FromStr for Person {
42
47
type Err = ParsePersonError ;
Original file line number Diff line number Diff line change 3
3
// Basically, this is the same as From. The main difference is that this should return a Result type
4
4
// instead of the target type itself.
5
5
// You can read more about it at https://doc.rust-lang.org/std/convert/trait.TryFrom.html
6
+ // Execute `rustlings hint from_str` or use the `hint` watch subcommand for a hint.
7
+
6
8
use std:: convert:: { TryFrom , TryInto } ;
7
9
8
10
#[ derive( Debug , PartialEq ) ]
Original file line number Diff line number Diff line change 4
4
//
5
5
// The goal is to make sure that the division does not fail to compile
6
6
// and returns the proper type.
7
+ // Execute `rustlings hint using_as` or use the `hint` watch subcommand for a hint.
7
8
8
9
// I AM NOT DONE
9
10
You can’t perform that action at this time.
0 commit comments