5

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?

jake77
  • 1,892
  • 2
  • 15
  • 22

1 Answers1

12

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.

ForceBru
  • 43,482
  • 10
  • 63
  • 98
  • 1
    Thank you, all is clear. I'm obviously not the first to ask, but at the beginning of the curve one sometimes struggles to find the right keywords... – jake77 Mar 13 '19 at 23:10