How to Use the Debug Trait for Debugging Output

Enable debug output for custom structs by adding #[derive(Debug)] and using the {:?} format specifier in println!.

Add the #[derive(Debug)] attribute above your struct definition and use the {:?} format specifier in your println! macro to print the instance. This enables Rust to automatically generate code that formats your struct's fields for debugging output.

#[derive(Debug)]
struct Rectangle {
    width: u32,
    height: u32,
}

fn main() {
    let rect1 = Rectangle {
        width: 30,
        height: 50,
    };

    println!("rect1 is {rect1:?}");
}