pub enum Pattern {
Wildcard,
Bind(VarId),
Const(Const),
Ctor {
variant: Arc<str>,
binds: Vec<(Arc<str>, Pattern)>,
},
At {
var: VarId,
inner: Box<Pattern>,
},
Or(Vec<Pattern>),
List {
items: Vec<Pattern>,
rest: Option<Option<VarId>>,
},
}Variants§
Wildcard
Bind(VarId)
Const(Const)
Ctor
Added(id, text) — variant names the constructor, and each field carries the pattern
matched against it.
A field’s pattern is usually a Pattern::Bind or a Pattern::Wildcard, which is what
Added(id, text) and Added(_) mean. It may be any pattern: Some(Added(id, text)) is a
Ctor whose one field is a Ctor.
At
whole @ Circle(r) — a name for the value, and a pattern that takes it apart.
The binder is irrefutable, so whether this matches is entirely inner’s question.
Or(Vec<Pattern>)
Circle(r) | Square(r) — one of several, and every alternative binds the same names.
The checker unifies the alternatives’ binders onto one set of variables, so the body reads
r without knowing which alternative matched. That is the rule that makes an or-pattern a
pattern rather than two arms sharing a body.
List
[], [x], [a, b], [first, *rest] — a list, taken apart.
items is one pattern per fixed element and rest is the optional tail binder. A pattern
with no rest matches a list of exactly items.len() elements; one with a rest matches
any list at least that long.
The tail is a binder rather than a pattern, and deliberately: [a, *[b, c]] is [a, b, c]
written twice over, so what it would add is a second spelling rather than a shape.
Implementations§
Source§impl Pattern
impl Pattern
Sourcepub fn binders(&self) -> Vec<VarId> ⓘ
pub fn binders(&self) -> Vec<VarId> ⓘ
Every variable this pattern binds, at any depth.
One method rather than a match at each of the three call sites, because those three were
Bind/Ctor/_ => {} — and a new pattern kind falling into the _ would have been a
silent miscount in the splitter’s variable supply and a false free variable in the plan’s
analysis. Neither would have failed a test until a program used one (docs/33 §33.5).
Sourcepub fn irrefutable(&self) -> bool
pub fn irrefutable(&self) -> bool
Whether this pattern matches every value of its type, so that no arm after it can run.
Only a binder and a wildcard do — and a list pattern of nothing but a tail, [*rest],
which is the one refutable-looking shape that refuses nothing.