Disabling Rust Unused Code Warnings

RustRustBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, the compiler provides a dead_code lint that warns about unused functions, but you can use attributes, such as #[allow(dead_code)], to disable the lint and prevent the warnings.

Note: If the lab does not specify a file name, you can use any file name you want. For example, you can use main.rs, compile and run it with rustc main.rs && ./main.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL rust(("`Rust`")) -.-> rust/FunctionsandClosuresGroup(["`Functions and Closures`"]) rust/FunctionsandClosuresGroup -.-> rust/function_syntax("`Function Syntax`") rust/FunctionsandClosuresGroup -.-> rust/expressions_statements("`Expressions and Statements`") subgraph Lab Skills rust/function_syntax -.-> lab-99340{{"`Disabling Rust Unused Code Warnings`"}} rust/expressions_statements -.-> lab-99340{{"`Disabling Rust Unused Code Warnings`"}} end

dead_code

The compiler provides a dead_code lint that will warn about unused functions. An attribute can be used to disable the lint.

fn used_function() {}

// `#[allow(dead_code)]` is an attribute that disables the `dead_code` lint
#[allow(dead_code)]
fn unused_function() {}

fn noisy_unused_function() {}
// FIXME ^ Add an attribute to suppress the warning

fn main() {
    used_function();
}

Note that in real programs, you should eliminate dead code. In these examples we'll allow dead code in some places because of the interactive nature of the examples.

Summary

Congratulations! You have completed the Dead_code lab. You can practice more labs in LabEx to improve your skills.

Other Rust Tutorials you may like