# Rust dbg! macro: inspect values with Debug

> Use Rust dbg! to print expressions to stderr, understand its return and move behavior, and compare compact {:?} with pretty {:#?} Debug output.

HTML: https://rustprint.com/debug/inspect-values/

`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.

**Inspecting an expression with dbg!**

```rust
let width = 6;
let area = dbg!(width * 4);
assert_eq!(area, 24);
```

## 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

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:

**Borrowing a value passed to dbg!**

```rust
let label = String::from("ready");
dbg!(&label);
assert_eq!(label, "ready");
```

## Debug output without dbg!

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

**Compact and pretty Debug**

```rust
#[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](/errors/doesnt-implement-debug/index.md).

## Common value-printing tasks

- [Print a Vec](/debug/print-vec/index.md) with compact or pretty Debug
- [Print a struct or array](/debug/print-struct/index.md) and derive `Debug` for your own struct

## Related references

- [Debug vs Display](/debug/debug-vs-display/index.md) for choosing the representation by audience
- [Pretty Debug with `{:#?}`](/debug/pretty-debug/index.md) for multi-line inspection
- [Formatting traits](/reference/formatting-traits/index.md) 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.

## Sources

- [dbg!](https://doc.rust-lang.org/std/macro.dbg.html)
- [Debug](https://doc.rust-lang.org/std/fmt/trait.Debug.html)

## Related RustPrint guides

- [Print a Vec in Rust](/debug/print-vec/index.md): Print a Rust Vec with {:?} or {:#?}, understand why {} fails, and format vector elements yourself when you need user-facing output.
- [Print structs and arrays in Rust](/debug/print-struct/index.md): Print a Rust struct by deriving Debug and using {:?} or {:#?}, and print arrays with the same Debug formatting syntax.
- [Rust E0277: type doesn't implement Debug](/errors/doesnt-implement-debug/index.md): Fix Rust E0277 when a value cannot be formatted with {:?} because its type does not implement std::fmt::Debug.
- [Rust format strings - syntax, width, precision, and traits](/format/format-strings/index.md): Read Rust format strings from {} through argument capture, width, precision, alignment, escaping, and formatting traits.
- [Rust formatting traits - Display, Debug, hex, binary, and more](/reference/formatting-traits/index.md): Map Rust format syntax such as {}, {:?}, {:x}, {:b}, and {:e} to the std::fmt traits Display, Debug, LowerHex, Binary, and LowerExp.
- [Debug vs Display in Rust](/debug/debug-vs-display/index.md): Choose between Rust Debug and Display based on programmer-facing inspection versus deliberate user-facing text.
- [Rust pretty print a struct with {:#?}](/debug/pretty-debug/index.md): Pretty print Rust structs, enums, maps, and nested values with {:#?}, derive Debug, and understand why Debug output is not a stable serialization format.
