The Debug Trait
Debug trait teaches a struct how to print itself out in a readable way when you're just trying to peek inside it.#[derive(Debug)]
struct Name {
field: Type,
}
println!("{:?}", value);
println!("{:#?}", value);
Deriving Debug
Adding #[derive(Debug)] above a struct definition automatically generates an implementation that lets you print the struct's fields for debugging purposes.
Example: Deriving Debug
// Auto-generate #[derive(Debug)] for the type below
#[derive(Debug)]
// Define a struct `Point` to group related fields together
struct Point {
x: i32,
y: i32,
}
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `p`, set to `Point { x: 3, y: 7 }`
let p = Point { x: 3, y: 7 };
// Print "{:?}" to the console, substituting in `p` (with a trailing newline)
println!("{:?}", p);
}
Login to try C/C++/Java/PHP code in the editor
Pretty-Printing with {:#?}
The alternate format specifier {:#?} prints a Debug value across multiple lines with indentation, making it easier to read for structs with many fields.
Example: Pretty-Printing with {:#?}
// Auto-generate #[derive(Debug)] for the type below
#[derive(Debug)]
// Define a struct `User` to group related fields together
struct User {
name: String,
age: u32,
}
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `user`, set to `User { name: String::from("Grace"), age: 40 }`
let user = User { name: String::from("Grace"), age: 40 };
// Print "{:#?}" to the console, substituting in `user` (with a trailing newline)
println!("{:#?}", user);
}
Login to try C/C++/Java/PHP code in the editor
Debug on Nested Structs
Debug derives recursively: if a struct contains another struct as a field, that inner struct must also derive Debug for the whole thing to be printable.
Example: Debug on Nested Structs
// Auto-generate #[derive(Debug)] for the type below
#[derive(Debug)]
// Define a struct `Address` to group related fields together
struct Address {
city: String,
}
// Auto-generate #[derive(Debug)] for the type below
#[derive(Debug)]
// Define a struct `Person` to group related fields together
struct Person {
name: String,
address: Address,
}
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `p`, set to `Person { name: String::from("Sam"), address: Address { city: String::from("Oslo") } }`
let p = Person { name: String::from("Sam"), address: Address { city: String::from("Oslo") } };
// Print "{:?}" to the console, substituting in `p` (with a trailing newline)
println!("{:?}", p);
}
Login to try C/C++/Java/PHP code in the editor
Debug vs Display
Debug ({:?}) is meant for developer-facing diagnostic output and can be auto-derived, while Display ({}) is meant for polished, user-facing text and must be implemented manually.
Example: Debug vs Display
// Auto-generate #[derive(Debug)] for the type below
#[derive(Debug)]
// Define a struct `Item` to group related fields together
struct Item {
name: String,
price: f64,
}
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `item`, set to `Item { name: String::from("Widget"), price: 9.99 }`
let item = Item { name: String::from("Widget"), price: 9.99 };
// Print "Debug view: {:?}" to the console, substituting in `item` (with a trailing newline)
println!("Debug view: {:?}", item);
}
Login to try C/C++/Java/PHP code in the editor
- Trying to print a struct with
{}when it only implementsDebug, notDisplay-- these use different placeholder syntax. - Forgetting to add
#[derive(Debug)]above a struct, then getting a compile error that the type doesn't implementDebug. - Not knowing about the pretty-print placeholder
{:#?}for nicely indented, multi-line struct output.
#[derive(Debug)]automatically implements theDebugtrait for a struct.{:?}is the format placeholder used to print a value that implementsDebug.{:#?}produces a nicely indented, multi-line pretty-print version of the same output.Debugoutput is meant for developers debugging their code, not for polished user-facing text.
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: