Debug is a Rust trait for producing programmer-oriented representations of values. Use {:?} when ordinary Display formatting with {} is unavailable or too limited; use {:#?} for multiline pretty printing. section 1.8
Choose a formatter based on the output you need:
- {} uses Display and usually produces prettier, more user-facing output. section 1.8
- {:?} uses Debug and usually reveals more information on one line. section 1.8
- {:#?} uses Debug with pretty printing, placing parts of a value on separate lines. section 1.8
Let the compiler suggest Debug
Printing the unit value () with {} produces an error because Display is not implemented. The compiler suggests {:?} or {:#?}; changing the formatter applies Debug instead. For custom types, #[derive(Debug)] usually supplies the implementation automatically, avoiding a hand-written fmt method. section 1.8 section 13.1.4
error[E0277]: `()` doesn't implement `std::fmt::Display` --> src\main.rs:3:41 | 3 | println!("This will not print: {}", doesnt_print); | ^^^^^^^^^^^^ `()` cannot be formatted with the default formatter | = help: the trait `std::fmt::Display` is not implemented for `()` = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead = note: required by `std::fmt::Display::fmt`
Inspect the compiler diagnostic that explains why {} fails for () and recommends Debug formatting. section 1.8
User {
name: "Mr. User",
user_number: 101,
}
Compare compact and pretty Debug output for a User value. section 1.8

