Skip to content

Commit

Permalink
add explanation of dbg! macro
Browse files Browse the repository at this point in the history
  • Loading branch information
mikemorris committed Aug 26, 2021
1 parent 6643c2a commit bd2c596
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/ch05-02-example-structs.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,28 @@ rect1 is Rectangle {
}
```

Now that we're familiar with deriving the `Debug` trait for helpful `println!`
formatting, let's take a look at a macro that builds on top of that functionality.
The `dbg!` macro (available in Rust 1.32.0 and later) can accept any argument
that implements the `Debug` trait, and will print helpful debugging context in
the same style as `println!("{:?#}", rect1)`, plus the filename and line number.

<span class="filename">Filename: src/main.rs</span>

```rust
#[derive(Debug)]
struct Rectangle {
width: u32,
height: u32,
}

fn main() {
let rect1 = Rectangle { width: 30, height: 50 };

dbg!(rect1);
}
```

Rust has provided a number of traits for us to use with the `derive` annotation
that can add useful behavior to our custom types. Those traits and their
behaviors are listed in Appendix C. We’ll cover how to implement these traits
Expand Down

0 comments on commit bd2c596

Please sign in to comment.