A literal pattern in Rust is a pattern that matches exactly a specific, hardcoded constant value. A match occurs if the scrutinee (the value being matched against) is structurally and strictly type-equal to the literal specified in the pattern. Literal patterns are inherently refutable, meaning they can fail to match. Because of this, they cannot be used alone in contexts that require irrefutable patterns (like standardDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
let bindings) and are primarily utilized in match expressions, if let, and while let constructs.
Syntax and Supported Types
Rust supports literal patterns for most primitive types. The syntax is identical to the literal expression syntax used elsewhere in the language. Because Rust is strictly typed, the scrutinee’s type must exactly match the literal pattern’s type, requiring separatematch blocks for different types.
Technical Mechanics
Exhaustiveness Checking The Rust compiler’s exhaustiveness checker fully understands the domains of primitive types. While types with vast domains likei32 or &str practically require a wildcard (_) or binding pattern to cover all unlisted values, finite domains can be matched exhaustively using only literal patterns. For example, providing both true and false for a bool, or explicitly listing all 256 possible values for a u8, fully satisfies the exhaustiveness checker and does not require a catch-all arm.
Strict Type Equality and Strings
Pattern matching requires strict type equality and does not perform implicit deref coercion. A string literal pattern ("...") has the type &str. Attempting to match an owned String (or &String) directly against a string literal pattern will result in a mismatched types compiler error. The scrutinee must be explicitly converted to a string slice before matching:
b"bytes") have the strict underlying type of a reference to a fixed-size array: &[u8; N]. While slice pattern semantics allow them to match against dynamically sized &[u8] slices, their inherent type dictates that they match the exact length N defined by the literal.
Floating-Point Restrictions
Floating-point literals (1.0, 3.14) are technically part of the literal pattern grammar, but their use is forbidden and will trigger compiler errors in modern Rust. This restriction exists because floating-point equality is inherently problematic due to NaN (Not-a-Number) representations, which do not equal themselves (NaN != NaN), violating the structural equality guarantees required by pattern matching.
Negative Literals
In the context of pattern matching, a minus sign - immediately preceding an integer literal is parsed as a single, unified literal pattern rather than a unary negation operator applied to an expression. This allows negative numbers to be matched directly without requiring guard clauses.
Master Rust with Deep Grasping Methodology!Learn More





