Sub topic 3
Debug: printing values for developers
This chapter assumes you know basic Rust syntax, how impl Trait for Type works, and how to use println! with format strings. It covers the Debug trait: what it is for, how to get it with derive, and the two output shapes it offers.
What Debug is for
Debug is the standard trait for showing a value to a developer. The {:?} format specifier requires it. Without it, printing a custom type with {:?} fails to compile.
The trait is meant for logs, test failures, and quick inspection. Its output is not meant for end users. That job belongs to Display, covered in a later chapter.
Getting Debug with derive
Writing the implementation yourself is rarely needed. The derive attribute asks the compiler to generate it:
#[derive(Debug)]
struct Point {
x: i32,
y: i32,
}
fn main() {
let p = Point { x: 1, y: 2 };
println!("{:?}", p);
println!("{:#?}", p);
}Running this prints:
Point { x: 1, y: 2 }
Point {
x: 1,
y: 2,
}The generated code prints the type name, then each field name and its value.
Compact and pretty output
The two specifiers use the same trait and differ only in layout.
| Specifier | Layout | Best for |
|---|---|---|
| `{:?}` | One line | Log lines, short values |
| `{:#?}` | Several indented lines | Nested structs, large collections |
The # is the "alternate" flag. It tells the formatter to produce the multi-line form. You do not write anything extra to support it, because derived implementations handle both.
The derive requirement is recursive
A derived Debug calls Debug on every field. If one field's type lacks it, compilation fails:
struct Inner;
#[derive(Debug)]
struct Outer {
inner: Inner, // error: `Inner` doesn't implement `Debug`
}The fix is to add #[derive(Debug)] to Inner as well. Standard types such as i32, String, Vec<T>, and Option<T> already implement Debug. For the generic ones, the contained type must implement it too.
Debug on enums
Derive works on enums too. Each variant prints its name, followed by any data it carries:
#[derive(Debug)]
enum Shape {
Circle { radius: f64 },
Square(f64),
Empty,
}
fn main() {
let shapes = vec![
Shape::Circle { radius: 1.5 },
Shape::Square(2.0),
Shape::Empty,
];
println!("{:?}", shapes);
}This prints [Circle { radius: 1.5 }, Square(2.0), Empty].
Don't rely on the exact text
The Rust documentation does not guarantee the exact output of derived Debug. It may change between compiler versions. Use it for humans to read. Do not parse it or compare it in program logic.
Exercise
Why does println!("{:?}", value) fail to compile when value is a struct with #[derive(Debug)] on it, but one of its fields has a type that does not implement Debug?
Show answer
A derived Debug calls Debug on each field, so every field's type must implement it. Add #[derive(Debug)] (or a manual implementation) to the field's type to fix the error.
Exercise
Which specifier would you use to print a deeply nested struct so that each field appears on its own indented line?
Show answer
{:#?}. The # flag selects the pretty, multi-line form of Debug.