File tree Expand file tree Collapse file tree 6 files changed +32
-20
lines changed Expand file tree Collapse file tree 6 files changed +32
-20
lines changed Original file line number Diff line number Diff line change 9
9
// Execute `rustlings hint errors1` or use the `hint` watch subcommand for a
10
10
// hint.
11
11
12
- // I AM NOT DONE
13
-
14
- pub fn generate_nametag_text ( name : String ) -> Option < String > {
12
+ pub fn generate_nametag_text ( name : String ) -> Result < String , String > {
15
13
if name. is_empty ( ) {
16
- // Empty names aren't allowed.
17
- None
14
+ Err ( String :: from ( "`name` was empty; it must be nonempty." ) )
18
15
} else {
19
- Some ( format ! ( "Hi! My name is {}" , name) )
16
+ Ok ( String :: from ( "Hi! My name is " ) + & name)
20
17
}
21
18
}
22
19
Original file line number Diff line number Diff line change 19
19
// Execute `rustlings hint errors2` or use the `hint` watch subcommand for a
20
20
// hint.
21
21
22
- // I AM NOT DONE
23
22
24
23
use std:: num:: ParseIntError ;
25
24
26
25
pub fn total_cost ( item_quantity : & str ) -> Result < i32 , ParseIntError > {
26
+ /*
27
27
let processing_fee = 1;
28
28
let cost_per_item = 5;
29
- let qty = item_quantity. parse :: < i32 > ( ) ;
29
+ let qty = item_quantity.parse::<i32>()? ;
30
30
31
31
Ok(qty * cost_per_item + processing_fee)
32
+ */
33
+ let processing_fee = 1 ;
34
+ let cost_per_item = 5 ;
35
+ let result = item_quantity. parse :: < i32 > ( ) ;
36
+ match result {
37
+ Ok ( qty) => Ok ( qty * cost_per_item + processing_fee) ,
38
+ Err ( e) => Err ( e)
39
+ }
32
40
}
33
41
34
42
#[ cfg( test) ]
Original file line number Diff line number Diff line change 7
7
// Execute `rustlings hint errors3` or use the `hint` watch subcommand for a
8
8
// hint.
9
9
10
- // I AM NOT DONE
11
10
12
11
use std:: num:: ParseIntError ;
13
12
14
- fn main ( ) {
13
+ fn main ( ) -> Result < ( ) , Box < dyn std :: error :: Error > > {
15
14
let mut tokens = 100 ;
16
15
let pretend_user_input = "8" ;
17
16
@@ -23,6 +22,7 @@ fn main() {
23
22
tokens -= cost;
24
23
println ! ( "You now have {} tokens." , tokens) ;
25
24
}
25
+ Ok ( ( ) )
26
26
}
27
27
28
28
pub fn total_cost ( item_quantity : & str ) -> Result < i32 , ParseIntError > {
Original file line number Diff line number Diff line change 3
3
// Execute `rustlings hint errors4` or use the `hint` watch subcommand for a
4
4
// hint.
5
5
6
- // I AM NOT DONE
7
6
8
7
#[ derive( PartialEq , Debug ) ]
9
8
struct PositiveNonzeroInteger ( u64 ) ;
@@ -17,7 +16,13 @@ enum CreationError {
17
16
impl PositiveNonzeroInteger {
18
17
fn new ( value : i64 ) -> Result < PositiveNonzeroInteger , CreationError > {
19
18
// Hmm...? Why is this only returning an Ok value?
20
- Ok ( PositiveNonzeroInteger ( value as u64 ) )
19
+ if value < 0 {
20
+ Err ( CreationError :: Negative )
21
+ } else if value == 0 {
22
+ Err ( CreationError :: Zero )
23
+ } else {
24
+ Ok ( PositiveNonzeroInteger ( value as u64 ) )
25
+ }
21
26
}
22
27
}
23
28
Original file line number Diff line number Diff line change 22
22
// Execute `rustlings hint errors5` or use the `hint` watch subcommand for a
23
23
// hint.
24
24
25
- // I AM NOT DONE
26
-
27
25
use std:: error;
28
26
use std:: fmt;
29
27
use std:: num:: ParseIntError ;
30
28
31
29
// TODO: update the return type of `main()` to make this compile.
32
- fn main ( ) -> Result < ( ) , Box < dyn ??? > > {
30
+ fn main ( ) -> Result < ( ) , Box < dyn error :: Error > > {
33
31
let pretend_user_input = "42" ;
34
32
let x: i64 = pretend_user_input. parse ( ) ?;
35
33
println ! ( "output={:?}" , PositiveNonzeroInteger :: new( x) ?) ;
Original file line number Diff line number Diff line change 9
9
// Execute `rustlings hint errors6` or use the `hint` watch subcommand for a
10
10
// hint.
11
11
12
- // I AM NOT DONE
13
-
14
12
use std:: num:: ParseIntError ;
15
13
16
14
// This is a custom error type that we will be using in `parse_pos_nonzero()`.
@@ -24,14 +22,20 @@ impl ParsePosNonzeroError {
24
22
fn from_creation ( err : CreationError ) -> ParsePosNonzeroError {
25
23
ParsePosNonzeroError :: Creation ( err)
26
24
}
27
- // TODO: add another error conversion function here.
28
- // fn from_parseint...
25
+ fn from_parseint ( err : ParseIntError ) -> ParsePosNonzeroError {
26
+ ParsePosNonzeroError :: ParseInt ( err)
27
+ }
29
28
}
30
29
31
30
fn parse_pos_nonzero ( s : & str ) -> Result < PositiveNonzeroInteger , ParsePosNonzeroError > {
32
31
// TODO: change this to return an appropriate error instead of panicking
33
32
// when `parse()` returns an error.
34
- let x: i64 = s. parse ( ) . unwrap ( ) ;
33
+ let x: i64 = match s. parse ( ) {
34
+ Ok ( x) => x,
35
+ Err ( e) => {
36
+ return Err ( ParsePosNonzeroError :: from_parseint ( e) ) ;
37
+ }
38
+ } ;
35
39
PositiveNonzeroInteger :: new ( x) . map_err ( ParsePosNonzeroError :: from_creation)
36
40
}
37
41
You can’t perform that action at this time.
0 commit comments