The Python 🐍 problem-solving bootcamp πŸš€ is starting soon. Join the second cohort now!

Today I learned why I should use the dbg! macro instead of the println! macro for debugging in Rust.

The Python 🐍 problem-solving bootcamp is starting soon. Join the second cohort now!

What is the dbg! macro in Rust?

The dgb! macro is a macro that is similar to println! but that is designed specifically for debugging. Not only does it print the values you pass it, but it also adds a tag with the name of the file and the line from where the macro was called.

Here is an example of using the dbg! macro:

fn main() {
    dbg!("hello, world!");
}

If you run your program with cargo run now, this is what you get:

❯ cargo run
   Compiling borrowing v0.1.0 (/Users/rodrigogs/Documents/rust/borrowing)
    Finished dev [unoptimized + debuginfo] target(s) in 0.10s
     Running `target/debug/borrowing`
[src/main.rs:2] "hello, world" = "hello, world"

Notice the last line of the output, that shows the argument to the macro, along with the line of code from where the macro was called. It may look redundant to have "hello, world" = "hello, world" displayed there, but that is because the macro will show the expression that was used as its argument and it will show the value of that expression.

Here is another example program:

fn main() {
    dbg!(3 + 3);
}

This shows:

❯ cargo run
# ...
[src/main.rs:2] 3 + 3 = 6

Or this program:

fn main() {
    let x = 3;
    dbg!(x);
}

Shows:

❯ cargo run
# ...
[src/main.rs:3] x = 3

So, this is how you use the dbg! macro in Rust!

That's it for now! Stay tuned and I'll see you around!

Take your Python 🐍 skills to the next level πŸš€

I write about Python every week. Join +16.000 others who are taking their Python 🐍 skills to the next level πŸš€, one email at a time.

Previous Post Next Post

Blog Comments powered by Disqus.