Skip to content

Commit 1444cb9

Browse files
author
1vk3y
committed
Restriction by trait Grade so that the ReportCard has and ONLY has two types: numeric and alphabetic.
1 parent e852e60 commit 1444cb9

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

solutions/quizzes/quiz3.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,30 @@
99

1010
use 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 {}",

0 commit comments

Comments
 (0)