Reputation: 171
In the Display chapter of Rust By Example, it describes Display
as "cleaner" than Debug
.
fmt::Debug
hardly looks compact and clean ...
fmt::Display
may be cleaner thanfmt::Debug
but ...
How is this? From what I see, you have to do more work and write more code to try and make Display
work while Debug
works right off the bat! Am I misunderstanding something about what "cleaner code" is, or is this a typo?
Upvotes: 3
Views: 687
Reputation: 632
Display's output is usually cleaner than Debug's, not the code to implement it. Debug's output is intended to be used for debug purposes, providing a less ambiguous output. Display's output is for user-facing output, so it's highly dependent on the meaning of your structure and that's why it cannot be derived.
For example, consider the following code:
fn main() {
// Note that \t is the TAB character
let output = "N\tO\tI\tC\tE";
println!("Debug: {:?}", output);
println!("Display: {}", output);
}
It will output:
Debug: "N\tO\tI\tC\tE"
Display: N O I C E
In this case, Debug will show the characters that the str (text) contains (as its more useful when debugging), while Display will just print them.
Upvotes: 10