Skip to content
Theory · Structs · lesson 6 of 8

Printing a struct: Debug

So far you have printed a struct field by field: anna.name, anna.age. When a program behaves oddly, you want to see the whole form at once, like a snapshot: the form's name and every field with its value.

The debug output {:?} has already shown you whole tuples and arrays. Let's try printing a struct the same way:

Rust
struct Point {
    x: i32,
    y: i32,
}

fn main() {
    let p = Point { x: 1, y: 2 };
    println!("{} {}", p.x, p.y);
    println!("{:?}", p); // ✗
}
TEXT
error[E0277]: `Point` doesn't implement `Debug`

The program does not compile. Rust knows how to print numbers, tuples and arrays, but it knows nothing about your struct. A struct has no default format: unlike Java or Python, Rust does not even print the type's name. This is a build error, not a panic: the compiler checks the trait behind each placeholder in advance.

#[derive(Debug)]

{:?} works with a type that has the Debug trait, that is, the ability to print itself in debug form. You could write it by hand, but usually you leave it to the compiler. An attribute is a #[…] line above a declaration that gives the compiler an instruction. The attribute #[derive(Debug)] means "derive Debug yourself". To derive here means to write the code from the struct's fields, not to print anything.

Rust
#[derive(Debug)]
struct Point {
    x: i32,
    y: i32,
}

fn main() {
    let p = Point { x: 1, y: 2 };
    println!("{} {}", p.x, p.y); // 1 2
    println!("{:?}", p); // Point { x: 1, y: 2 }
}

Let's walk through the example line by line:

  1. #[derive(Debug)] above struct Point asks the compiler to write debug printing for Point.
  2. The first println! prints the fields one by one: 1 2.
  3. {:?} prints Point { x: 1, y: 2 }: the type's name and each field with its value, much like the literal.

What else to know about a derived Debug:

  • {:#?} prints the same thing but puts each field on its own line, which makes a big struct easier to read;
  • derive works only if every field has Debug: numbers, String and tuples do;
  • the compiler does not count printing with {:?} as reading a field. If the program did not read p.x and p.y separately, the build would give warning: fields `x` and `y` are never read.

This is where the snapshot analogy falls short. You can show a snapshot of a form to anyone, but {:?} is a format for programmers. It is the same for every type and does not suit a screen that a user will see.

Common mistake

People expect #[derive(Debug)] to give printing with {} too:

Rust
#[derive(Debug)]
struct Point {
    x: i32,
    y: i32,
}

fn main() {
    let p = Point { x: 1, y: 2 };
    println!("{} {}", p.x, p.y);
    println!("{}", p); // ✗
}
TEXT
error[E0277]: `Point` doesn't implement `std::fmt::Display`

The program does not compile. {} needs a different trait, Display: it is the view for users, and the type's author should decide what it looks like. That is why the compiler does not write Display for you, and {} never falls back to {:?}. For debugging, write {:?}.

Key points

  • {:?} prints a struct only if it has Debug; without it the build fails with error E0277.
  • #[derive(Debug)] above a struct derives Debug for you; {:?} prints Point { x: 1, y: 2 }.
  • derive(Debug) does not give {}: that needs the Display trait.