In the Display chapter of Rust By Example, it describes Display as "cleaner" than Debug.
fmt::Debughardly looks compact and clean ...
fmt::Displaymay be cleaner thanfmt::Debugbut ...
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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With