book icon indicating copy to clipboard operation
book copied to clipboard

appearance of dead_code in Debug

Open kimpham54 opened this issue 3 years ago • 0 comments

  • I have searched open and closed issues and pull requests for duplicates, using these search terms:
    • field is never read
    • dead_code
  • I have checked the latest main branch to see if this has already been fixed, in this file:
    • https://github.com/rust-lang/book/blob/main/src/ch05-02-example-structs.md

URL to the section(s) of the book with this problem:

  • https://doc.rust-lang.org/book/ch05-02-example-structs.html Description of the problem:
  • Not a problem but a nice to have for rust newbies like myself. The appearance of the dead_code warning first appears in this section when running
#[derive(Debug)]
struct Rectangle {
    width: u32,
    height: u32,
}

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

    println!("rect1 is {:?}", rect1);
}

Here is my warning message:

warning: fields `width` and `height` are never read
 --> src/main.rs:3:5
  |
2 | struct Rectangle {
  |        --------- fields in this struct
3 |     width: u32,
  |     ^^^^^^^^^^
4 |     height: u32,
  |     ^^^^^^^^^^^
  |
  = note: `#[warn(dead_code)]` on by default
  = note: `Rectangle` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis

Suggested fix:

  • it would be nice to explain this for the first time in this chapter that you might see this dead_code message, but it's OK as mentioned in this ticket https://github.com/rust-lang/book/issues/2761 which will prevent future bug reports

kimpham54 avatar Oct 30 '22 13:10 kimpham54