Skip to main content

Fixed: "warning: fields `name` and `age` are never read" in Rust

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

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:

Terminal
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:

src/main.rs, which defines a User struct with a name and an age that derives Debug, creates a user named Ada aged 36, and prints the whole struct in the Debug format.

src/main.rs
#[derive(Debug)]
struct User {
name: String,
age: u32,
}

fn main() {
let user = User {
name: "Ada".to_owned(),
age: 36,
};
println!("{user:?}");
}

Run it:

Terminal
cargo run -q
Output
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 }

A zsh terminal where cargo run -q warns that the fields name and age are never read, notes that the derived Debug impl is ignored during dead code analysis, and then prints User with name Ada and 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: "User has a derived impl for the trait Debug, but this is intentionally ignored during dead code analysis". {:?} runs that derived Debug code, 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 Clone and Debug implementations 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:

src/main.rs
#[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​

Terminal
cargo run -q
Output
Ada is 36

A zsh terminal where cargo run -q prints Ada is 36 with no warning.

No warning, and the program printed Ada is 36.

How to Avoid It​

  • Don't count {:?} output as using a field: derived Debug and Clone code 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​

SymptomLikely causeFix
warning: fields `name` and `age` are never read, although {:?} prints themDead code analysis ignores derived Debug codeRead the fields where your code needs them, or remove them
warning: field `age` is never read after the code reads nameOne field is still unreadRead 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​