Printing with Derived Traits
#[derive(Debug)] struct Rectangle { width: u32, height: u32, } fn main() { let rect1 = Rectangle { width: 30, height: 50, }; println!("rect1 is {:?}", rect1); }
Putting the specifierĀ :?
Ā inside the curly brackets tellsĀ println!
Ā we want to use an output format calledĀ Debug
. TheĀ Debug
Ā trait enables us to print our struct in a way that is useful for developers so we can see its value while weāre debugging our code.
RustĀ doesĀ include functionality to print out debugging information, but we have to explicitly opt in to make that functionality available for our struct. To do that, we add the outer attributeĀ #[derive(Debug)]
Ā just before the struct definition,
To format the output we use {:#?}