How to disable unused code warnings in Rust?

Andrew Wagner picture Andrew Wagner · Sep 16, 2014 · Viewed 89.4k times · Source
struct SemanticDirection;

fn main() {}
warning: struct is never used: `SemanticDirection`
 --> src/main.rs:1:1
  |
1 | struct SemanticDirection;
  | ^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: #[warn(dead_code)] on by default

I will turn these warnings back on for anything serious, but I am just tinkering with the language and this is driving me bats.

I tried adding #[allow(dead_code)] to my code, but that did not work.

Answer

Arjan picture Arjan · Sep 16, 2014

You can either:

  • Add an allow attribute on a struct, module, function, etc.:

    #[allow(dead_code)]
    struct SemanticDirection;
    
  • Add a crate-level allow attribute; notice the !:

    #![allow(dead_code)]
    
  • Pass it to rustc:

    rustc -A dead_code main.rs
    
  • Pass it using cargo via the RUSTFLAGS environment variable:

    RUSTFLAGS="$RUSTFLAGS -A dead_code" cargo build