File tree Expand file tree Collapse file tree 1 file changed +17
-5
lines changed
Expand file tree Collapse file tree 1 file changed +17
-5
lines changed Original file line number Diff line number Diff line change 99
1010use std:: fmt:: Display ;
1111
12- // Make the struct generic over `T`.
13- struct ReportCard < T > {
14- // ^^^
12+ // Grade Trait
13+ trait Grade { }
14+ //^^^^^^^^^^^^
15+
16+ // Implements the trait for `f32` (numeric, e.g. 1.0 -> 5.5)
17+ impl Grade for f32 { }
18+ //^^^^^^^^^^^^^^^^^^^
19+
20+ // Implements the trait for `str` (alphabetic, A+ -> F-)
21+ impl Grade for & str { }
22+ //^^^^^^^^^^^^^^^^^^^^
23+
24+ // Make the struct generic over `T: Grade`.
25+ struct ReportCard < T : Grade > {
26+ // ^^^^^^^^^^
1527 grade : T ,
1628 // ^
1729 student_name : String ,
1830 student_age : u8 ,
1931}
2032
2133// To be able to print the grade, it has to implement the `Display` trait.
22- impl < T : Display > ReportCard < T > {
23- // ^^^^^^^ require that `T` implements `Display`.
34+ impl < T : Display + Grade > ReportCard < T > {
35+ // ^^^^^^^^^^^^^^^ require that `T` implements `Display` and `Grade `.
2436 fn print ( & self ) -> String {
2537 format ! (
2638 "{} ({}) - achieved a grade of {}" ,
You can’t perform that action at this time.
0 commit comments