Rust dbg! macro: inspect values with Debug
dbg!(expression) prints the source location, expression text, and its Debug value to stderr, then returns the evaluated value unchanged. Use it for quick inspection while debugging.
let width = 6;
let area = dbg!(width * 4);
assert_eq!(area, 24);What dbg! prints
Section titled “What dbg! prints”dbg! uses the Debug representation of the value and includes the source location plus the expression text. It is active in release builds too.
Its exact text format is not a stable interface, so do not parse it or use it as application output.
dbg! returns the value
Section titled “dbg! returns the value”Because dbg! returns the evaluated expression, it can be inserted into an expression while you inspect an intermediate result.
For non-Copy values, calling dbg!(value) moves the value into the macro and back out. Borrow when the result is not being rebound:
let label = String::from("ready");
dbg!(&label);
assert_eq!(label, "ready");Debug output without dbg!
Section titled “Debug output without dbg!”Debug is the formatting trait for programmer-facing output. Use {:?} for compact output and {:#?} for the alternate, pretty representation.
#[derive(Debug)]
struct Point {
x: i32,
y: i32,
}
let point = Point { x: 3, y: 4 };
let compact = format!("{point:?}");
let pretty = format!("{point:#?}");If {:?} produces E0277 because your type lacks the trait, see type doesn’t implement Debug.
Common value-printing tasks
Section titled “Common value-printing tasks”- Print a Vec with compact or pretty Debug
- Print a struct or array and derive
Debugfor your own struct
Related references
Section titled “Related references”- Debug vs Display for choosing the representation by audience
- Pretty Debug with
{:#?}for multi-line inspection - Formatting traits for the trait behind each format suffix
For durable application diagnostics, use a logging or tracing facility. This page covers inspection output only; it does not claim to diagnose general compiler errors.