Poperton
Poperton

Reputation: 2147

How to disable unused variable warning in Rust?

According to this answer, #[allow(dead_code)] should work, but it doesn't

fn main() {
    #[allow(dead_code)]
    let x = 0;
}

Upvotes: 19

Views: 19928

Answers (3)

phocks
phocks

Reputation: 3273

Put #![allow(unused)] at the top of the file (note the !).

Upvotes: 15

Poperton
Poperton

Reputation: 2147

The correct is

fn main() {
    #[allow(unused_variables)]
    let x = 0;
}

Upvotes: 18

Peter Hall
Peter Hall

Reputation: 58735

These are different lints. dead_code refers to unused code at the item level, e.g. imports, functions and types. unused_variables refers to variables that are never accessed.

You can also cover both cases with #[allow(unused)].

Upvotes: 25

Related Questions