Skip to content

Commit bd2c596

Browse files
committed
add explanation of dbg! macro
1 parent 6643c2a commit bd2c596

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/ch05-02-example-structs.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,28 @@ rect1 is Rectangle {
252252
}
253253
```
254254

255+
Now that we're familiar with deriving the `Debug` trait for helpful `println!`
256+
formatting, let's take a look at a macro that builds on top of that functionality.
257+
The `dbg!` macro (available in Rust 1.32.0 and later) can accept any argument
258+
that implements the `Debug` trait, and will print helpful debugging context in
259+
the same style as `println!("{:?#}", rect1)`, plus the filename and line number.
260+
261+
<span class="filename">Filename: src/main.rs</span>
262+
263+
```rust
264+
#[derive(Debug)]
265+
struct Rectangle {
266+
width: u32,
267+
height: u32,
268+
}
269+
270+
fn main() {
271+
let rect1 = Rectangle { width: 30, height: 50 };
272+
273+
dbg!(rect1);
274+
}
275+
```
276+
255277
Rust has provided a number of traits for us to use with the `derive` annotation
256278
that can add useful behavior to our custom types. Those traits and their
257279
behaviors are listed in Appendix C. We’ll cover how to implement these traits

0 commit comments

Comments
 (0)