Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is Display "cleaner" than Debug?

Tags:

rust

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 than fmt::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?

like image 241
aRustyDev Avatar asked Oct 20 '25 05:10

aRustyDev


1 Answers

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.

like image 108
pheki Avatar answered Oct 22 '25 05:10

pheki