Skip to main content

Derive, Default and struct update syntax

`#[derive(...)]` asks the compiler to generate a standard trait implementation from the shape of your type. `Debug` gives you `{:?}` printing, `Clone` gives explicit copies, `PartialEq` gives `==`, and `Default` gives a zero-ish starting value.

Derive only works when every field also implements the trait, which is why an error here usually points at one field rather than your struct.

Struct update syntax (`..Default::default()`) fills in the fields you did not mention, which keeps configuration structs readable as they grow.

Printing a type that has no Debug

struct Point { x: i32 }

fn main() {
    let p = Point { x: 1 };
    println!("{p:?}"); // error[E0277]
}

error[E0277]: `Point` doesn't implement `Debug`

Add `#[derive(Debug)]` above the struct. For custom, user-facing output implement `Display` by hand instead.

Run it and change it

The editor below is live: edit anything and the real compiler output updates by itself.

Your Rust