Given this:
fn main() {
let variable = [0; 15];
}
The Rust compiler produces this warning:
= note: #[warn(unused_variables)] on by default
= note: to avoid this warning, consider using `_variable` instead
What's the difference between variable
and _variable
?
The difference is an underscore at the front, which causes the Rust compiler to allow it to be unused. It is kind of a named version of the bare underscore
_
which can be used to ignore a value.However,
_name
acts differently than_
. The plain underscore drops the value immediately while_name
acts like any other variable and drops the value at the end of the scope.An example of how it does not act exactly the same as a plain underscore:
prints the following (playground):