A tuple pattern is a structural pattern used to destructure and match against tuple types. It allows developers to bind individual elements of a tuple to distinct variables, ignore specific elements, or assert the shape of the data by matching the tuple’s arity and the types of its constituent elements.Documentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
Syntax and Arity Matching
A tuple pattern consists of a comma-separated list of sub-patterns enclosed in parentheses. By default, the arity (number of elements) of the tuple pattern must strictly match the arity of the tuple expression being evaluated. Special syntactic rules apply to tuples of arity 0 and 1:- Arity 0 (Unit Type): The empty tuple pattern
()is used to match the unit type. - Arity 1: A single-element tuple pattern must include a trailing comma to distinguish it from a parenthesized pattern. For example,
(x,)is a tuple pattern, whereas(x)is simply the patternxenclosed in parentheses.
Ignoring Elements
You can selectively ignore elements within a tuple pattern using the wildcard pattern (_) for single elements, or the rest pattern (..) for contiguous sequences of elements.
The rest pattern (..) can only be used once per tuple pattern to avoid ambiguity in arity resolution.
Refutability
The refutability of a tuple pattern is entirely dependent on its sub-patterns.- Irrefutable: If all sub-patterns within the tuple pattern are irrefutable (e.g., variable bindings or wildcards), the tuple pattern itself is irrefutable and can be used in
letstatements or function parameters. - Refutable: If any sub-pattern is refutable (e.g., matching a specific literal or an enum variant), the entire tuple pattern becomes refutable and must be used in constructs that handle branching, such as
match,if let, orwhile let.
Nesting
Tuple patterns are fully composable. They can be nested inside other tuple patterns, struct patterns, or enum patterns, and can conversely contain any valid sub-pattern.Binding Modes
When destructuring a tuple, binding modes (ref, ref mut, mut) can be applied to individual sub-patterns to control how the memory of the tuple’s elements is accessed or borrowed. If a reference to a tuple is matched, the ref binding mode is implicitly applied to its elements via Rust’s match ergonomics.
Master Rust with Deep Grasping Methodology!Learn More





