Display
fmt::Debug
hardly looks compact and clean, so it is often advantageous to
customize the output appearance. This is done by manually implementing
fmt::Display
, which uses the {}
print marker. Implementing it
looks like this:
fmt::Display
may be cleaner than fmt::Debug
but this presents
a problem for the std
library. How should ambiguous types be displayed?
For example, if the std
library implemented a single style for all
Vec<T>
, what style should it be? Would it be either of these two?
Vec<path>
:/:/etc:/home/username:/bin
(split on:
)Vec<number>
:1,2,3
(split on,
)
No, because there is no ideal style for all types and the std
library
doesn't presume to dictate one. fmt::Display
is not implemented for Vec<T>
or for any other generic containers. fmt::Debug
must then be used for these
generic cases.
This is not a problem though because for any new container type which is
not generic, fmt::Display
can be implemented.
So, fmt::Display
has been implemented but fmt::Binary
has not, and therefore
cannot be used. std::fmt
has many such traits
and each requires
its own implementation. This is detailed further in std::fmt
.
Activity
After checking the output of the above example, use the Point2D
struct as a
guide to add a Complex
struct to the example. When printed in the same
way, the output should be:
Display: 3.3 + 7.2i
Debug: Complex { real: 3.3, imag: 7.2 }