pub enum Pattern {
Show 19 variants
Wildcard,
Literal(Literal),
Identifier(String),
QualifiedName(Vec<String>),
Tuple(Vec<Pattern>),
List(Vec<Pattern>),
Struct {
name: String,
fields: Vec<StructPatternField>,
has_rest: bool,
},
TupleVariant {
path: Vec<String>,
patterns: Vec<Pattern>,
},
Range {
start: Box<Pattern>,
end: Box<Pattern>,
inclusive: bool,
},
Or(Vec<Pattern>),
Rest,
RestNamed(String),
AtBinding {
name: String,
pattern: Box<Pattern>,
},
WithDefault {
pattern: Box<Pattern>,
default: Box<Expr>,
},
Mut(Box<Pattern>),
Ok(Box<Pattern>),
Err(Box<Pattern>),
Some(Box<Pattern>),
None,
}Expand description
Patterns for destructuring and matching values.
Patterns are used in match expressions, let bindings, function parameters, and other contexts where values need to be destructured or tested against a structure. Ruchy supports a rich pattern language including literals, destructuring, ranges, and alternative patterns.
§Pattern Types
- Wildcard:
_matches anything - Literal: Matches exact values
- Identifier: Binds matched value to a name
- Tuple/List: Destructures sequences
- Struct: Destructures struct fields
- Or: Matches any of several patterns
- Rest: Captures remaining elements
§Examples
ⓘ
match value {
0 => "zero", // Literal pattern
1..=10 => "small", // Range pattern
Some(x) => format!("value: {x}"), // Enum pattern
[first, ..rest] => "list", // List pattern with rest
_ => "other", // Wildcard
}Variants§
Wildcard
Literal(Literal)
Identifier(String)
QualifiedName(Vec<String>)
Tuple(Vec<Pattern>)
List(Vec<Pattern>)
Struct
TupleVariant
Range
Or(Vec<Pattern>)
Rest
RestNamed(String)
AtBinding
WithDefault
Mut(Box<Pattern>)
Ok(Box<Pattern>)
Err(Box<Pattern>)
Some(Box<Pattern>)
None
Implementations§
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Pattern
impl<'de> Deserialize<'de> for Pattern
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
impl StructuralPartialEq for Pattern
Auto Trait Implementations§
impl Freeze for Pattern
impl RefUnwindSafe for Pattern
impl Send for Pattern
impl Sync for Pattern
impl Unpin for Pattern
impl UnwindSafe for Pattern
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more