How to disable unused code warnings in Rust?
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 theRUSTFLAGS
environment variable:RUSTFLAGS="$RUSTFLAGS -A dead_code" cargo build
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);
Put these two lines on the top of the file.
#![allow(dead_code)]
#![allow(unused_variables)]