jake77
jake77

Reputation: 2034

underscore in Rust: "consider using"

Rust newbie here. When providing a parameter and leaving it unused in a function declaration (e.g. when learning Rust...) the compiler warns about the fact that the variable is unused in the scope, and proposes to consider putting an underline before it. Doing so, the warning disappears.

warning: unused variable: `y`
--> src/main.rs:23:29
   |
23 | fn another_function(x: i32, y: i32) {
   |                             ^ help: consider using `_y` instead
   |
   = note: #[warn(unused_variables)] on by default

why? How is the variable treated differently then?

Upvotes: 5

Views: 2018

Answers (1)

ForceBru
ForceBru

Reputation: 44888

It's just a convention: Rust doesn't emit a warning if a variable whose name starts with an underscore is not used because sometimes you may need a variable that won't be used anywhere else in your code.

Upvotes: 12

Related Questions