How to quiet a warning for a single statement in Rust?

To silence warnings you have to add the allow(warning_type) attribute to the affected expression or any of its parents. If you only want to silence the warning on one specific expression, you can add the attribute to that expression/statement:

fn main() {
    #[allow(unused_variables)]
    let not_used = 27;

    #[allow(path_statements)]
    std::io::stdin;

    println!("hi!");
}

However, the feature of adding attributes to statements/expressions (as opposed to items, like functions) is still a bit broken. In particular, in the above code, the std::io::stdin line still triggers a warning. You can read the ongoing discussion about this feature here.


Often it is not necessary to use an attribute though. Many warnings (like unused_variables and unused_must_use) can be silenced by using let _ = as the left side of your statement. In general, any variable that starts with an underscore won’t trigger unused-warnings.

Leave a Comment