_) is an irrefutable pattern that matches any value without binding it to an identifier. It instructs the compiler to explicitly discard the matched data, satisfying pattern exhaustiveness checks while preventing “unused variable” warnings.
Binding, Ownership, and Drop Semantics
Unlike named variables or variables prefixed with an underscore (e.g., _var), the standalone wildcard _ never creates a binding. Because no binding occurs, the wildcard pattern does not move, consume, or take ownership of the matched value.
Copy types:
_ not creating a binding is its effect on the drop semantics of temporary values. When a temporary expression is assigned to a wildcard, the value is dropped immediately at the end of the statement. In contrast, assigning a temporary to a named binding (even one prefixed with an underscore) extends the value’s lifetime to the end of the enclosing scope. This distinction is a common footgun in RAII (Resource Acquisition Is Initialization) patterns, such as managing lock guards.
match Expressions
Because _ is irrefutable (it cannot fail to match), it functions as a universal catch-all. When placed as the final arm of a match expression, it guarantees that all possible variants of a type are accounted for, satisfying the compiler’s strict exhaustiveness requirements.
Tired of Poor Rust Skills? Fix That With Deep Grasping!Learn More





