Skip to main content
The wildcard pattern (_) 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.
Conversely, an identifier prefixed with an underscore acts as a standard binding and will trigger move semantics for non-Copy types:
A critical mechanical consequence of _ 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.
Structural Destructuring The wildcard pattern can be nested within compound patterns to selectively ignore specific elements of tuples, arrays, enums, or structs during destructuring. Tuples and Arrays:
Enums:
Structs:
Exhaustiveness in 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.
Function and Closure Parameters The wildcard can replace parameter names in function, trait method, or closure signatures. This indicates that an argument is structurally required by the type signature but is intentionally ignored in the implementation scope.
Tired of Poor Rust Skills? Fix That With Deep Grasping!Learn More