Open In App

Rust – dead_code lint

Last Updated : 09 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Rust is a systems programming language that has some unique features which include warning if there are unused variables or unused functions. To do this, the Rust compiler provides the users with a dead code lint which warns us about unused codes, and by the usage of the #[allow(dead_code)], it eliminates any unused variables/functions error.

Example 1:

Rust




// Rust code 
fn outer_func_one() {
println!("this is the first outer function");
  }
   
fn outer_func_two(){
println!("this is the second outer function");
    }
  
fn main() {
    outer_func_one();
}


Output:

 

Explanation:

In this example, we have created two functions: outer_func_one() and outer_func_two(). We are calling the outer_func_one from the main method but we never use the outer_func_two. Due to this Rust compiler throws us a warning that the outer_func_two() is never used and suggests we handle warn dead code by default error.

Example 2:

Rust




fn outer_func_one() {
println!("this is the first outer function");
  }
   
#[allow(dead_code)]
fn outer_func_two(){
println!("this is the second outer function");
    }
  
fn main() {
    outer_func_one();
}


Output:

 

Explanation:

Continuing from Example 1, handle the warn dead code by adding #[allow(dead_code)] attribute, the lints are disabled.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads