Disabling Rust Unused Code Warnings

Beginner

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.

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.