Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

underscore in Rust: "consider using"

Tags:

rust

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?

like image 442
jake77 Avatar asked Dec 18 '22 18:12

jake77


1 Answers

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.

like image 140
ForceBru Avatar answered Jan 14 '23 08:01

ForceBru