Small Programs · 10 min · free with an account

Inspecting Values

Lesson preview · about 30%

Debug output is evidence for development, not part of the program's user-facing result.

Core idea

dbg! returns its argument and writes diagnostic output to stderr.

Start with the idea

The {:?} formatter uses the Debug trait. dbg! prints the expression, source location, and value to stderr, then returns the value unchanged. Borrow with dbg!(&value) when you want to inspect an owned value without moving it. Remove noisy probes or replace them with deliberate logging before shipping.

A useful debug probe answers a specific question. Before running the program, predict the value at a boundary: after parsing, before a branch, or after a collection update. Put dbg! there, run the smallest input that exposes the issue, and compare the printed value with the prediction. If the value is already wrong, move the probe earlier; if it is right, move it later. This narrows the search instead of filling the terminal with unrelated output.

println! writes to standard output, which users or another program may be reading as the result. dbg!

Keep learning, for free

The rest of this lesson is waiting for you.

Create a free account to read the complete explanation, see the infographic, explore the code, and try the exercises. All 742 lessons and algorithm steps are included.

No payment details. Your account opens the complete Academy.

← All lessons in Rust Basics