Exerpad
🔥 0
0 XP
Exercise 5 · Chapter: TraitsRequired+50 XP

Display Trait

Create a Color struct with fields r, g, b (all u8). Implement the Display trait so it prints in the format rgb(R, G, B). Print a color with values 255, 128, 0.

Expected output:

Color: rgb(255, 128, 0)

Hints:

  • Add use std::fmt; at the top
  • Implement impl fmt::Display for Color
  • Use write!(f, "rgb({}, {}, {})", self.r, self.g, self.b)
rust
fn main() {
}
Output
Run your code to see the output here.