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.
Read the code
let items = vec!["crab", "rust"];
dbg!(&items);
println!("{} items", items.len());Borrowing lets dbg! inspect the Vec without consuming it.
Practice starter
let name = String::from("Ferris");
dbg!(name);
println!("{name}");
Work through it
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! writes to standard error and includes the source expression and location, so it is better suited to a temporary inspection. The macro returns the value it receives. For an owned String or Vec, passing the value can move it into the macro; pass &value to inspect without giving it away. {:?} requires Debug, and a struct can usually obtain that trait with #[derive(Debug)].
Try the lesson's name example twice: first observe the move error, then change the probe to dbg!(&name) and run it again. Record what appeared on stderr and what remained on stdout. After finding the cause, remove the probe or replace it with intentional diagnostics that have a stable purpose. A good debugging habit includes a final clean run, because experimental output should not become an accidental part of the program's interface.
Debug output helps inspect a running program, but it is not automatically an explanation. Print the value immediately before the branch or calculation you distrust, state what you expected, and compare. {:?} uses the Debug trait and often exposes structure; {} uses Display when a user-facing representation exists. An added print can change timing, especially around concurrency, so it is evidence rather than a proof that every execution behaves the same way.
Choose one wrong result in a small example. Write a hypothesis before adding a print, then place exactly one observation that could confirm or refute it. Remove or gate temporary diagnostics once the issue is understood. For a persistent behavior, write a test for the input that failed. If the compiler rejects a value because it lacks Debug, derive or implement that trait only when exposing its fields is appropriate. This turns debugging into a sequence of questions rather than a flood of logs.
Try it
Inspect an intermediate value and keep the program's stdout stable.
What success looks like: The debug probe appears on stderr and the value remains usable.
Check your understanding
Which edit keeps name usable?
Reveal the answer
- dbg!(&name)
- dbg!(mut name)
- let copy = dbg!(name)
Pass a shared reference so the debug probe does not take ownership.
Learn this lesson with AI
Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.
I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools. Lesson: Inspecting Values (courseId "basics", lessonId "debugging-values") Link: https://crabrix.com/learn/basics/debugging-values/ Goal: dbg! returns its argument and writes diagnostic output to stderr. Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=basics/debugging-values) and I will write the code there. Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.