I am learning Rust, and came across the fact that adding an underscore at the beginning of a variable name will make the compiler not warn if it is unused. I am wondering why that feature exists, since unused variables are frowned upon.
I can see several reasons:
#[must_use]
type, but in your specific case, you know you can safely ignore the value. It is possible to use a _
pattern for that (which is not a variable binding, it's a pattern of its own, but this is probably where the underscore prefix convention comes from), but you might want to document why you ignore the value, or what that value is. This is particularly common in tests in my experience.used_underscore_binding
lint._
for this use-case, as _
is not a variable binding and the value would not be dropped at the end of the enclosing block as with variable binding.