Skip to content

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.

EXAMPLE: CI-CHECKEDTEST ASSERTED · examples/format-output/src/reference_examples.rs#debug-expression
let width = 6;
let area = dbg!(width * 4);
assert_eq!(area, 24);

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.

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:

EXAMPLE: CI-CHECKEDTEST ASSERTED · examples/format-output/src/reference_examples.rs#debug-borrow
let label = String::from("ready");
dbg!(&label);
assert_eq!(label, "ready");

Debug is the formatting trait for programmer-facing output. Use {:?} for compact output and {:#?} for the alternate, pretty representation.

EXAMPLE: CI-CHECKEDTEST ASSERTED · examples/format-output/src/reference_examples.rs#debug-formats
#[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.

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.