File tree Expand file tree Collapse file tree 18 files changed +55
-52
lines changed Expand file tree Collapse file tree 18 files changed +55
-52
lines changed Original file line number Diff line number Diff line change 99// Execute `rustlings hint errors1` or use the `hint` watch subcommand for a
1010// hint.
1111
12- // I AM NOT DONE
13-
14- pub fn generate_nametag_text ( name : String ) -> Option < String > {
12+ fn generate_nametag_text ( name : String ) -> Result < String , String > {
1513 if name. is_empty ( ) {
16- // Empty names aren't allowed.
17- None
14+ // Empty names aren't allowed
15+ Err ( "`name` was empty; it must be nonempty." . into ( ) )
1816 } else {
19- Some ( format ! ( "Hi! My name is {}" , name ) )
17+ Ok ( format ! ( "Hi! My name is {name}" ) )
2018 }
2119}
2220
Original file line number Diff line number Diff line change 1919// Execute `rustlings hint errors2` or use the `hint` watch subcommand for a
2020// hint.
2121
22- // I AM NOT DONE
2322
2423use std:: num:: ParseIntError ;
2524
2625pub fn total_cost ( item_quantity : & str ) -> Result < i32 , ParseIntError > {
2726 let processing_fee = 1 ;
2827 let cost_per_item = 5 ;
29- let qty = item_quantity. parse :: < i32 > ( ) ;
28+ // let qty = item_quantity.parse::<i32>();
29+ let qty = item_quantity. parse :: < i32 > ( ) ?;
3030
3131 Ok ( qty * cost_per_item + processing_fee)
3232}
Original file line number Diff line number Diff line change 77// Execute `rustlings hint errors3` or use the `hint` watch subcommand for a
88// hint.
99
10- // I AM NOT DONE
11-
10+ use std:: error:: Error ;
1211use std:: num:: ParseIntError ;
1312
14- fn main ( ) {
13+ fn main ( ) -> Result < ( ) , Box < dyn Error > > {
1514 let mut tokens = 100 ;
1615 let pretend_user_input = "8" ;
1716
@@ -23,6 +22,7 @@ fn main() {
2322 tokens -= cost;
2423 println ! ( "You now have {} tokens." , tokens) ;
2524 }
25+ Ok ( ( ) )
2626}
2727
2828pub fn total_cost ( item_quantity : & str ) -> Result < i32 , ParseIntError > {
Original file line number Diff line number Diff line change 33// Execute `rustlings hint errors4` or use the `hint` watch subcommand for a
44// hint.
55
6- // I AM NOT DONE
76
87#[ derive( PartialEq , Debug ) ]
98struct PositiveNonzeroInteger ( u64 ) ;
@@ -17,7 +16,12 @@ enum CreationError {
1716impl PositiveNonzeroInteger {
1817 fn new ( value : i64 ) -> Result < PositiveNonzeroInteger , CreationError > {
1918 // Hmm...? Why is this only returning an Ok value?
20- Ok ( PositiveNonzeroInteger ( value as u64 ) )
19+ match value {
20+ v if v < 0 => Err ( CreationError :: Negative ) ,
21+ 0 => Err ( CreationError :: Zero ) ,
22+ _ => Ok ( Self ( value as u64 ) ) ,
23+ }
24+
2125 }
2226}
2327
Original file line number Diff line number Diff line change 2222// Execute `rustlings hint errors5` or use the `hint` watch subcommand for a
2323// hint.
2424
25- // I AM NOT DONE
2625
2726use std:: error;
2827use std:: fmt;
2928use std:: num:: ParseIntError ;
3029
3130// TODO: update the return type of `main()` to make this compile.
32- fn main ( ) -> Result < ( ) , Box < dyn ??? > > {
31+ fn main ( ) -> Result < ( ) , Box < dyn error :: Error > > {
3332 let pretend_user_input = "42" ;
3433 let x: i64 = pretend_user_input. parse ( ) ?;
3534 println ! ( "output={:?}" , PositiveNonzeroInteger :: new( x) ?) ;
Original file line number Diff line number Diff line change 99// Execute `rustlings hint errors6` or use the `hint` watch subcommand for a
1010// hint.
1111
12- // I AM NOT DONE
1312
1413use std:: num:: ParseIntError ;
1514
@@ -26,13 +25,20 @@ impl ParsePosNonzeroError {
2625 }
2726 // TODO: add another error conversion function here.
2827 // fn from_parseint...
28+ fn from_parse_int ( err : ParseIntError ) -> Self {
29+ Self :: ParseInt ( err)
30+ }
2931}
3032
33+
3134fn parse_pos_nonzero ( s : & str ) -> Result < PositiveNonzeroInteger , ParsePosNonzeroError > {
3235 // TODO: change this to return an appropriate error instead of panicking
3336 // when `parse()` returns an error.
34- let x: i64 = s. parse ( ) . unwrap ( ) ;
35- PositiveNonzeroInteger :: new ( x) . map_err ( ParsePosNonzeroError :: from_creation)
37+ // let x: i64 = s.parse().unwrap();
38+ // PositiveNonzeroInteger::new(x).map_err(ParsePosNonzeroError::from_creation)
39+ s. parse :: < i64 > ( )
40+ . map_err ( ParsePosNonzeroError :: from_parse_int)
41+ . and_then ( |x| PositiveNonzeroInteger :: new ( x) . map_err ( ParsePosNonzeroError :: from_creation) )
3642}
3743
3844// Don't change anything below this line.
Original file line number Diff line number Diff line change 66// Execute `rustlings hint generics1` or use the `hint` watch subcommand for a
77// hint.
88
9- // I AM NOT DONE
109
1110fn main ( ) {
12- let mut shopping_list: Vec < ? > = Vec :: new ( ) ;
11+ let mut shopping_list: Vec < & ' static str > = Vec :: new ( ) ;
1312 shopping_list. push ( "milk" ) ;
1413}
Original file line number Diff line number Diff line change 66// Execute `rustlings hint generics2` or use the `hint` watch subcommand for a
77// hint.
88
9- // I AM NOT DONE
109
11- struct Wrapper {
12- value : u32 ,
10+ struct Wrapper < T > {
11+ value : T ,
1312}
1413
15- impl Wrapper {
16- pub fn new ( value : u32 ) -> Self {
14+ impl < T > Wrapper < T > {
15+ fn new ( value : T ) -> Self {
1716 Wrapper { value }
1817 }
1918}
Original file line number Diff line number Diff line change 88// Execute `rustlings hint lifetimes1` or use the `hint` watch subcommand for a
99// hint.
1010
11- // I AM NOT DONE
1211
13- fn longest ( x : & str , y : & str ) -> & str {
12+ fn longest < ' a > ( x : & ' a str , y : & ' a str ) -> & ' a str {
1413 if x. len ( ) > y. len ( ) {
1514 x
1615 } else {
Original file line number Diff line number Diff line change 66// Execute `rustlings hint lifetimes2` or use the `hint` watch subcommand for a
77// hint.
88
9- // I AM NOT DONE
109
1110fn longest < ' a > ( x : & ' a str , y : & ' a str ) -> & ' a str {
1211 if x. len ( ) > y. len ( ) {
@@ -22,6 +21,6 @@ fn main() {
2221 {
2322 let string2 = String :: from ( "xyz" ) ;
2423 result = longest ( string1. as_str ( ) , string2. as_str ( ) ) ;
24+ println ! ( "The longest string is '{}'" , result) ;
2525 }
26- println ! ( "The longest string is '{}'" , result) ;
2726}
You can’t perform that action at this time.
0 commit comments