Formatting
We've seen that formatting is specified via a format string:
format!("{}", foo)
->"3735928559"
format!("0x{:X}", foo)
->"0xDEADBEEF"
format!("0o{:o}", foo)
->"0o33653337357"
The same variable (foo
) can be formatted differently depending on which
argument type is used: X
vs o
vs unspecified.
This formatting functionality is implemented via traits, and there is one trait
for each argument type. The most common formatting trait is Display
, which
handles cases where the argument type is left unspecified: {}
for instance.
You can view a full list of formatting traits and their argument
types in the std::fmt
documentation.
Activity
Add an implementation of the fmt::Display
trait for the Color
struct above
so that the output displays as:
RGB (128, 255, 90) 0x80FF5A
RGB (0, 3, 254) 0x0003FE
RGB (0, 0, 0) 0x000000
Two hints if you get stuck:
- You may need to list each color more than once,
- You can pad with zeros to a width of 2 with
:0>2
.