A path pattern is a pattern that matches a value against a named constant, a unit struct, or a unit enum variant resolved through a module path. It evaluates toDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
true if the matched value is structurally equal to the value represented by the path. Unlike identifier patterns, path patterns do not introduce new variable bindings into the local scope; they strictly perform structural equality checks against pre-existing items in the module tree.
Syntax
A path pattern follows standard Rust path resolution syntax. It supports relative paths, absolute paths from the crate root, generic arguments, and fully qualified paths for trait items.Resolution Mechanics
The Rust compiler distinguishes between path patterns and identifier patterns based on scope resolution:- Multi-segment paths: Any pattern containing a double-colon
::(e.g.,Module::Item,Option::None, or<T as Trait>::CONSTANT) is unambiguously parsed as a path pattern. - Single-segment paths: If a pattern consists of a single identifier (e.g.,
Item), the compiler queries the current scope’s value namespace.- If the identifier resolves to a constant, unit struct, or unit enum variant, it is treated as a path pattern.
- If the identifier resolves to another item in the value namespace (such as a tuple struct, tuple enum variant, or a struct with fields), the compiler emits a compilation error (e.g.,
E0532orE0533), as these items require tuple or struct patterns. - If the identifier does not resolve to any item in the value namespace, it is treated as an identifier pattern and binds the matched value to a new variable.
Resolvable Items
A path pattern must resolve to one of the following item types:- Constants: Items defined via the
constkeyword. - Unit Enum Variants: Variants of an
enumthat hold no data. - Unit Structs: Structs defined without fields.
Semantics and Constraints
- Structural Equality: When a path pattern resolves to a constant, the pattern matching does not call the
PartialEq::eqmethod. Executing user-defined trait methods during pattern matching would allow arbitrary code execution and side effects. Instead, the compiler relies on intrinsic structural equality. - Trait Requirements: For a constant of a custom type to be valid in a path pattern, its type must have
#[derive(PartialEq, Eq)]. This macro derives the hidden, compiler-internalStructuralPartialEqandStructuralEqtraits, which authorize the compiler to perform a structural comparison (field-by-field) of the data. - Structural Match Restrictions: If a constant contains types that do not implement the structural match traits (such as raw pointers, floating-point numbers, or types with custom, non-derived
PartialEqimplementations), the compiler will emit a structural match error. - Refutability: The refutability of a path pattern depends entirely on the item it resolves to. A path pattern is irrefutable if it resolves to a unit struct, a variant of a single-variant enum, or a constant whose type is a singleton (a type with only one possible value, such as
()), meaning any match against it is inherently exhaustive. It is refutable if it resolves to a variant of a multi-variant enum or a constant of a type with multiple possible values (such as an integer or a multi-variant enum).
Master Rust with Deep Grasping Methodology!Learn More





