An identifier pattern binds a matched value to a local variable. It consists of a variable name (the identifier), optionalDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
ref and/or mut modifiers to alter the binding mode, and an optional @ operator to bind the identifier while simultaneously matching the value against a subpattern.
When evaluated, an identifier pattern unconditionally matches the value (unless constrained by a refutable subpattern) and introduces a new variable into the current lexical scope.
Syntax
Components and Explicit Binding Modes
The behavior of the identifier pattern is dictated by its modifiers, which control how the value is bound to the identifier.IDENTIFIER: The name of the variable. It must be a valid Rust identifier. The wildcard_is not an identifier pattern; it is a distinct wildcard pattern that does not bind a variable.mut: Alters the binding mode so that the newly bound variable is mutable. It does not change whether the value is moved, copied, or borrowed; it only affects the mutability of the resulting local variable.ref: Alters the binding mode to bind the variable by reference rather than by value.refcreates a shared reference (&T).ref mutcreates a mutable reference (&mut T).
@(Subpattern Binding): The at-binding operator binds the value to theIDENTIFIERonly if the value also successfully matches the subsequentPattern. The refutability of the entire identifier pattern is determined entirely by this subpattern. If the subpattern is irrefutable, the@pattern is also irrefutable and is valid in standardletstatements or function parameters. If the subpattern is refutable, it must be used in refutable contexts likeif letormatcharms.
Match Ergonomics (Default Binding Modes)
If an identifier pattern does not explicitly use theref or ref mut keywords, its binding mode is determined by the compiler’s default binding modes (Match Ergonomics). When a reference is matched against a structural pattern, the binding mode transitions to a reference mode.
Any identifier pattern encountered while in a reference binding mode will automatically bind by reference:
- If the context is a shared reference (
&T), the identifier implicitly binds asref(yielding&T). - If the context is a mutable reference (
&mut T), the identifier implicitly binds asref mut(yielding&mut T).
Syntax Visualization
Standard Value Binding Binds the value directly to the identifier.@)
Binds the identifier to the value while evaluating an irrefutable subpattern. This is valid in standard let bindings.
@)
Binds the identifier to the value while evaluating a refutable subpattern constraint. This requires a refutable context.
Shadowing and Scope
If theIDENTIFIER shares a name with a variable already present in the enclosing scope, the identifier pattern shadows the existing variable for the duration of the new scope. It does not mutate or reassign the outer variable.
Master Rust with Deep Grasping Methodology!Learn More





