377
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.

Ryan M
  • 15,686
  • 29
  • 53
  • 64
Andrew Wagner
  • 20,197
  • 19
  • 75
  • 95

8 Answers8

593

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
    
Serid
  • 326
  • 5
  • 12
Arjan
  • 17,401
  • 2
  • 52
  • 47
86

Another way to disable this warning is to prefix the identifier by _:

struct _UnusedStruct {
    _unused_field: i32,
}

fn main() {
    let _unused_variable = 10;
}

This can be useful, for instance, with an SDL window:

let _window = video_subsystem.window("Rust SDL2 demo", 800, 600);

Prefixing with an underscore is different from using a lone underscore as the name. Doing the following will immediately destroy the window, which is unlikely to be the intended behavior.

let _ = video_subsystem.window("Rust SDL2 demo", 800, 600);
Shepmaster
  • 326,504
  • 69
  • 892
  • 1,159
antoyo
  • 9,835
  • 3
  • 43
  • 77
  • 3
    That "assigning to underscore will destroy it" behavior seems odd (though I don't doubt you're correct). Do you have a reference for it? – Michael Anderson Jul 06 '18 at 05:29
  • 8
    @MichaelAnderson See "RAII. You might want to have a variable exist for its destructor side effect, but not use it otherwise. It is not possible to use simply _ for this use-case, as _ is not a variable binding and the value would be dropped at the end of the statement." from https://stackoverflow.com/a/48361729/109618 – David J. Jul 07 '18 at 21:26
  • 1
    using `let _ =` the value would be dropped at the end of the statement, not the end of the block – nicolas Sep 26 '20 at 08:04
  • If you want to read more about why, the reason is that the `X` in `let X = Y` is an irrefutable pattern (i.e. it's like a `match` arm that can be proved to never be wrong at compile time) and, like with refutable patterns, `_` is a wildcard that doesn't bind anything to a variable. That's why and how you can do `let (x, y) = foo();` and other sorts of unpacking like that. It's just another kind of irrefutable pattern. – ssokolow Dec 21 '20 at 21:16
25

Making the code public also stops the warnings; you'll need to make the enclosing mod's public too.

This makes sense when you're writing a library: your code is "unused" internally because it's intended to be used by client code.

Victor Basso
  • 5,108
  • 5
  • 39
  • 58
  • 1
    I think this does not work if the crate contains both a main.rs and a lib.rs, and the main.rs does not use the function under question. – hoijui Sep 12 '21 at 09:37
22

Put these two lines on the top of the file.

#![allow(dead_code)]
#![allow(unused_variables)]
M. Hamza Rajput
  • 5,489
  • 1
  • 33
  • 28
5

also as an addition: rust provides four levels of lints (allow, warn, deny, forbid).

https://doc.rust-lang.org/rustc/lints/levels.html#lint-levels

Muhammed Moussa
  • 3,365
  • 28
  • 19
3

You can always disable unused variables/functions by adding an (_) to the variable name, like so:

let _variable = vec![0; 10];
3

For unused functions, you should make the function public, but watch out. If the struct isn't public, then you'll still get the error as in here:

//this should be public also
struct A{
   A{}
}

impl A {
    pub fn new() -> A {

    }
}

Or if you don't want it to be public, you should put #[allow(unused)]

PPP
  • 783
  • 15
  • 52
0

using features

#[cfg(feature = "dead_code")]

note: "dead_code" can be replaced by any word.

davidsbro
  • 2,758
  • 4
  • 21
  • 33
jmzhou
  • 1
  • I have never seen cfg/feature used like this. Can you explain how this works, or is it documented somewhere? – MB-F Jan 14 '22 at 22:11