Skip to main content

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.

A grouped pattern in Rust consists of a pattern enclosed in parentheses to explicitly control parsing precedence. It matches identically to the inner pattern it encloses, serving solely to disambiguate complex pattern combinations during syntactic analysis, particularly those involving the alternation operator (|), identifier binding (@), or reference patterns (& and &mut).

Syntax

( PATTERN )

Mechanics and Precedence

In Rust’s pattern matching grammar, certain operators have lower precedence than others. The grouped pattern forces the compiler’s parser to evaluate the enclosed pattern as a single, atomic unit before applying surrounding pattern operators. A grouped pattern delegates all matching, destructuring, and variable binding semantics directly to its inner pattern. It does not introduce new types, nor does it change the memory layout of the matched value.

Alternation and Binding

The most common structural conflict occurs between the subpattern binding operator (@) and the alternation operator (|). The | operator has lower precedence than @.
let value = 1;

// Without grouping: Parses as `(x @ 1) | 2`. 
// This fails to compile because the variable `x` is not bound in all alternatives.
/*
match value {
    x @ 1 | 2 => {} 
    _ => {}
}
*/

// With grouping: Parses as `x @ (1 | 2)`.
// The grouped pattern forces the alternation to be parsed first.
// `x` successfully binds to the matched value of the entire grouped pattern.
match value {
    x @ (1 | 2) => {} 
    _ => {}
}

Reference Patterns

Grouped patterns are also used to control the application of reference semantics (& or &mut) when combined with alternation. Note that binding modes like ref and mut apply exclusively to identifier patterns and cannot be applied to grouped patterns. Attempting to use a binding mode on a grouped pattern (e.g., ref (x | y)) results in a syntax error.
let reference_value = &1;

// Matches a reference to either 1 or 2.
// The `&` reference pattern applies to the entire grouped alternation.
match reference_value {
    &(1 | 2) => {}
    _ => {}
}

// Without grouping, this would parse as `(&1) | 2`, 
// resulting in a type mismatch error between `&i32` and `i32`.

Grouped Patterns vs. Tuple Patterns

A critical syntactic distinction in Rust is the difference between a grouped pattern and a single-element tuple pattern. The parser differentiates them based on the presence of a trailing comma.
  • ( PATTERN ): A grouped pattern. Modifies precedence. Matches the exact type of the inner pattern.
  • ( PATTERN , ): A tuple pattern of arity 1. Matches a 1-tuple (e.g., (i32,)).
let value = 5;
let tuple_value = (5,);

// Grouped pattern: Matches an i32
let (x) = value; 

// Tuple pattern: Matches an (i32,)
let (y,) = tuple_value; 
Master Rust with Deep Grasping Methodology!Learn More