Sub topic 6
Debug: output meant for programmers
This chapter assumes you know basic Rust syntax, how println! placeholders work, and what a trait is. Deriving Debug for your own types comes in the next chapter.
Two traits for two audiences
Rust has two main traits for turning a value into text. Display (the {} placeholder) is for end users. Debug (the {:?} placeholder) is for programmers who want to see what a value contains.
They are separate because the two goals conflict. A user wants 42 or hello. A programmer wants to see the structure, such as quotes around strings, Some(...) around options, and brackets around lists. Many types have an obvious Debug form but no single sensible Display form.
Using `{:?}`
The standard library implements Debug for numbers, strings, Option, Vec, tuples, and many other types. It also implements Debug for nested combinations of them.
fn main() {
println!("{:?}", vec![Some(1), None]);
println!("{:?}", "a\"b");
println!("{:?}", 'x');
println!("{:?}", (1, "two", 3.0));
println!("{:?}", ());
}This prints:
[Some(1), None]
"a\"b"
'x'
(1, "two", 3.0)
()Notice what Debug adds:
- Strings keep their quotes, and special characters are escaped.
"a\"b"shows the backslash that a Display print would hide. - Characters appear in single quotes.
- The float
3.0keeps its decimal point. Display prints1.0_f64as1, but Debug prints1.0, so you can tell floats from integers. - The unit value
()has Debug output but no Display implementation.
Debug works with the other placeholder forms
Debug combines with the argument forms from earlier chapters. You can write {name:?} for a captured variable or {0:?} for a positional argument.
fn main() {
let words = vec!["alpha", "beta"];
let count = words.len();
println!("{words:?} has {count} items");
println!("{0:?} and {0}", "hi");
}The second line prints "hi" and hi, which shows the difference between the two traits for the same value.
The common error
If a type does not implement the trait a placeholder asks for, the compiler stops with an error. Using {} on a vector is the usual case:
fn main() {
let v = vec![1, 2, 3];
println!("{}", v);
}The error says Vec<i32> doesn't implement std::fmt::Display, and the help text suggests using {:?} instead. The reverse also happens. A struct you wrote has no Debug until you add it, so {:?} fails on it.
Choosing between them
| Situation | Use |
|---|---|
| Text shown to a user | `{}` |
| Logging, debugging, test output | `{:?}` |
| A type with no Display | `{:?}` |
Exercise
What does println!("{:?}", "hi") print, and how does it differ from println!("{}", "hi")?
Show answer
The Debug form prints "hi" with the double quotes included. The Display form prints hi with no quotes. Debug shows the value as a string literal, with escapes where needed.
Exercise
Why does println!("{}", vec![1, 2, 3]) fail to compile, and what is the smallest change that fixes it?
Show answer
Vec<i32> does not implement Display, because there is no single obvious way to show a list to a user. Changing {} to {:?} fixes it, since Vec<i32> implements Debug. The output is [1, 2, 3].