Fixed: "warning: fields `name` and `age` are never read" in Rust
Rust warned that two fields of a struct were never read, although the program had just printed both of them. It printed the struct with {:?}, which runs the code that #[derive(Debug)] wrote, and the compiler leaves derived Debug code out when it works out what's used.
What I Was Doing
I was writing a small Rust example that reads the claims of a JSON Web Token into a struct without verifying the token. The struct derived Debug and serde's Deserialize, and the program printed it with println!("{claims:?}"). It printed every claim, but before that cargo run warned warning: fields `sub`, `role`, and `exp` are never read.
The example below shows the same warning with nothing else in the way: a struct with two fields that derives Debug, printed with {:?}.
What I Expected
The program to build without a warning, because printing the struct shows both fields.
What Happened Instead
Create a project:
cargo new demo
cd demo
cargo new demo creates a project in a folder named demo, with its code in src/main.rs, and cd demo moves into it.
Replace the contents of src/main.rs with:

#[derive(Debug)]
struct User {
name: String,
age: u32,
}
fn main() {
let user = User {
name: "Ada".to_owned(),
age: 36,
};
println!("{user:?}");
}
Run it:
cargo run -q
warning: fields `name` and `age` are never read
--> src/main.rs:3:5
|
2 | struct User {
| ---- fields in this struct
3 | name: String,
| ^^^^
4 | age: u32,
| ^^^
|
= note: `User` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis
= note: `#[warn(dead_code)]` (part of `#[warn(unused)]`) on by default
User { name: "Ada", age: 36 }

The program still ran and printed User { name: "Ada", age: 36 }, so this is a warning, not an error. But the compiler counted both fields as never read.
What I Checked
- The output shows both values, so the program does use them, but only through
{:?}. - The first note under the warning names the cause: "
Userhas a derived impl for the traitDebug, but this is intentionally ignored during dead code analysis".{:?}runs that derivedDebugcode, so as far as the analysis is concerned, nothing reads the fields. - The second note names the lint,
dead_code, which is on by default. The rustc documentation of the lint: "The dead_code lint detects unused, unexported items." And: "Dead code may signal a mistake or unfinished code. To silence the warning for individual items, prefix the name with an underscore such as _foo." It ends with: "Otherwise consider removing the unused code." - The Rust 1.57.0 release notes list the change behind the note: "Ignore derived
CloneandDebugimplementations during dead code analysis."
So the warning was right: apart from the debug printout, nothing in the program reads name or age.
The Fix
Read the fields in the code that needs them. This program wants to print the user, so it prints the two fields itself:
#[derive(Debug)]
struct User {
name: String,
age: u32,
}
fn main() {
let user = User {
name: "Ada".to_owned(),
age: 36,
};
println!("{} is {}", user.name, user.age);
}
Only the last line changed: println!("{} is {}", user.name, user.age); reads both fields.
If your code never needs a field, remove it. If you keep one on purpose without reading it, start its name with an underscore, as the lint documentation suggests: renamed to _age, the field got no warning.
The Result
cargo run -q
Ada is 36

No warning, and the program printed Ada is 36.
How to Avoid It
- Don't count
{:?}output as using a field: derivedDebugandClonecode doesn't count. - When the compiler says a field is never read, check whether your code needs it. If it doesn't, remove it.
- Read the notes under a warning: here, the first one named the cause.
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
warning: fields `name` and `age` are never read, although {:?} prints them | Dead code analysis ignores derived Debug code | Read the fields where your code needs them, or remove them |
warning: field `age` is never read after the code reads name | One field is still unread | Read it, remove it, or rename it _age |
FAQ
Does the warning stop the build?
No. The program built and printed User { name: "Ada", age: 36 }. The 1.57.0 release notes add that the change "will break some builds that set #![deny(dead_code)]": that attribute turns the warning into an error.
Doesn't deriving Deserialize count as reading the fields?
No. In my JWT example, the struct also derived serde's Deserialize, and the compiler still warned about all three fields. Deserializing fills the fields in; it doesn't read them.
Conclusion
Printing a struct with {:?} doesn't count as reading its fields, because dead code analysis ignores derived Debug code. When Rust says a field is never read, read it where your code needs it, or remove it.
Additional Resources
- dead-code, in the rustc book's list of warn-by-default lints.
- The Rust 1.57.0 release notes.
