A slice pattern is a structural pattern matching mechanism in Rust used to destructure and match against contiguous sequences of elements, specifically arrays (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.
[T; N]) and dynamically sized slices (&[T]). Vectors (Vec<T>) cannot be matched directly using slice patterns; they must first be borrowed or dereferenced into a slice (e.g., &v[..] or v.as_slice()). The pattern evaluates the sequence based on its length, the values of specific elements, or a combination of both, allowing for precise extraction of elements and subslices.
Core Syntax and Mechanics
Slice patterns are enclosed in square brackets[] and can contain literal values, variable bindings, wildcards (_), and the rest pattern (..).
Exact Length Matching
You can match a sequence of a specific, known length. If the sequence length does not match the pattern length, the match arm fails.The Rest Pattern (..)
The .. token represents a variable-length sequence of elements. It allows you to match the beginning, the end, or both ends of a slice while ignoring the remaining elements.
Rule: The .. token can only be used once per slice pattern. Using it multiple times (e.g., [1, .., 2, ..]) results in a compiler error due to ambiguity.
Subslice Binding (@ ..)
You can capture the elements matched by the rest pattern into a new variable using the @ binding operator. The resulting bound variable will be a slice (&[T]) containing the captured elements.
To illustrate how bindings are evaluated without shadowing or unreachable arms, consider these distinct match statements:
Technical Nuances
Exhaustiveness Checking When matching against a dynamically sized slice (&[T]), the compiler cannot know the length of the slice at compile time. Therefore, slice patterns on &[T] are inherently non-exhaustive unless they include a catch-all arm (_) or an unconditional rest pattern ([..]).
Conversely, when matching against a fixed-size array ([T; N]), the compiler knows the exact length. You can write exhaustive matches without a catch-all if you cover all possible value permutations for that specific length.
Match Ergonomics and Binding Modes
When you match a reference to a slice (&[T]), Rust’s match ergonomics automatically dereference the slice and bind the individual elements by reference (&T).
[T; N]) directly, the elements are moved into the bindings unless explicitly prefixed with ref.
Master Rust with Deep Grasping Methodology!Learn More





