The dbg! macro is fantastic for quick debugging because it:
- Shows the expression itself, not just the value
- Prints the file and line number
- Returns the value, so you can use it inline
let x = 5;
let y = dbg!(x * 2); // Prints: [src/main.rs:3] x * 2 = 10
// y is now 10
Compare this to println!:
let x = 5;
println!("{}", x * 2); // Prints: 10
// You have to know what this value represents
The dbg! approach is especially useful when debugging expressions in the middle of a chain:
let result = values
.iter()
.map(|x| dbg!(x * 2)) // See each multiplication
.filter(|x| dbg!(x > &5)) // See each filter decision
.sum::<i32>();
Just remember to remove dbg! calls before committing — they’re meant for development only!