Traits and Generics
Derive Debug
Printing a Struct
#[derive(Debug)] generates a debug formatter so a struct can be printed with {:?}.
Program
Play the program to print a point using its derived Debug output.
derive_debug.rs
#[derive(Debug)]
struct Point {
x: i32,
y: i32,
}
fn main() {
let p = Point { x: 1, y: 2 };
println!("{p:?}");
}
derive
`#[derive(Debug)]` auto-generates trait code.
Debug
The `Debug` trait enables developer-facing formatting.
{:?}
`{:?}` prints the derived debug representation.