File tree Expand file tree Collapse file tree 4 files changed +23
-15
lines changed Expand file tree Collapse file tree 4 files changed +23
-15
lines changed Original file line number Diff line number Diff line change 6
6
// Execute `rustlings hint generics1` or use the `hint` watch subcommand for a
7
7
// hint.
8
8
9
- // I AM NOT DONE
9
+
10
10
11
11
fn main ( ) {
12
- let mut shopping_list: Vec < ? > = Vec :: new ( ) ;
12
+ let mut shopping_list: Vec < & str > = Vec :: new ( ) ;
13
13
shopping_list. push ( "milk" ) ;
14
14
}
Original file line number Diff line number Diff line change 6
6
// Execute `rustlings hint generics2` or use the `hint` watch subcommand for a
7
7
// hint.
8
8
9
- // I AM NOT DONE
10
9
11
- struct Wrapper {
12
- value : u32 ,
10
+
11
+ struct Wrapper < T > {
12
+ value : T ,
13
13
}
14
14
15
- impl Wrapper {
16
- pub fn new ( value : u32 ) -> Self {
15
+ impl < T > Wrapper < T > {
16
+ pub fn new ( value : T ) -> Self {
17
17
Wrapper { value }
18
18
}
19
19
}
Original file line number Diff line number Diff line change 3
3
// Execute `rustlings hint options1` or use the `hint` watch subcommand for a
4
4
// hint.
5
5
6
- // I AM NOT DONE
6
+
7
7
8
8
// This function returns how much icecream there is left in the fridge.
9
9
// If it's before 10PM, there's 5 pieces left. At 10PM, someone eats them
@@ -13,7 +13,13 @@ fn maybe_icecream(time_of_day: u16) -> Option<u16> {
13
13
// value of 0 The Option output should gracefully handle cases where
14
14
// time_of_day > 23.
15
15
// TODO: Complete the function body - remember to return an Option!
16
- ???
16
+ if time_of_day < 22 {
17
+ Some ( 5 )
18
+ } else if time_of_day > 23 {
19
+ None
20
+ } else {
21
+ Some ( 0 )
22
+ }
17
23
}
18
24
19
25
#[ cfg( test) ]
@@ -33,7 +39,7 @@ mod tests {
33
39
fn raw_value ( ) {
34
40
// TODO: Fix this test. How do you get at the value contained in the
35
41
// Option?
36
- let icecreams = maybe_icecream ( 12 ) ;
42
+ let icecreams = maybe_icecream ( 12 ) . unwrap ( ) ;
37
43
assert_eq ! ( icecreams, 5 ) ;
38
44
}
39
45
}
Original file line number Diff line number Diff line change 16
16
//
17
17
// Execute `rustlings hint quiz3` or use the `hint` watch subcommand for a hint.
18
18
19
- // I AM NOT DONE
20
19
21
- pub struct ReportCard {
22
- pub grade : f32 ,
20
+
21
+ use std:: fmt:: Display ;
22
+
23
+ pub struct ReportCard < T : Display > {
24
+ pub grade : T ,
23
25
pub student_name : String ,
24
26
pub student_age : u8 ,
25
27
}
26
28
27
- impl ReportCard {
29
+ impl < T : Display > ReportCard < T > {
28
30
pub fn print ( & self ) -> String {
29
31
format ! ( "{} ({}) - achieved a grade of {}" ,
30
32
& self . student_name, & self . student_age, & self . grade)
@@ -52,7 +54,7 @@ mod tests {
52
54
fn generate_alphabetic_report_card ( ) {
53
55
// TODO: Make sure to change the grade here after you finish the exercise.
54
56
let report_card = ReportCard {
55
- grade : 2.1 ,
57
+ grade : "A+" ,
56
58
student_name : "Gary Plotter" . to_string ( ) ,
57
59
student_age : 11 ,
58
60
} ;
You can’t perform that action at this time.
0 commit comments