pub struct Pat { /* private fields */ }Expand description
A pattern: the left-hand side of one match arm, or a part of one.
Patterns are built bottom-up with the constructors below and describe
values of the type they are checked against, in terms of the
Signature the Model gives that
type. Which constructor fits which signature:
| Signature | Patterns |
|---|---|
Sum | variant |
Product | product |
Bool | bool |
Int, BigInt | int, range |
Char | char, char_range |
Opaque | lit |
Array, Slice | slice, slice_rest |
| any | wild, or |
A binding such as x or x @ p is, for matching purposes, _ or p.
§Node numbering
Diagnostics about part of a pattern (see
Redundant) name the part by its preorder index
in the arm’s pattern: the root is node 0, and each node is followed by
its children (fields, alternatives, or slice elements, prefix before
suffix) left to right, each child’s whole subtree before the next child.
§Examples
use match_lang::Pat;
// `Some(1 | 2..=5)`, with `Some` as variant 1 of its sum type.
let pat = Pat::variant(1, [Pat::or([Pat::int(1), Pat::range(2, 5)])]);
assert_eq!(pat.to_string(), "#1(1 | 2..=5)");Implementations§
Source§impl Pat
impl Pat
Sourcepub fn wild() -> Pat
pub fn wild() -> Pat
The wildcard _, which matches every value of any type. A binding
pattern (x) is a wildcard to the checker.
§Examples
use match_lang::Pat;
assert_eq!(Pat::wild().to_string(), "_");Sourcepub fn variant(index: u32, fields: impl IntoIterator<Item = Pat>) -> Pat
pub fn variant(index: u32, fields: impl IntoIterator<Item = Pat>) -> Pat
Variant index of a Sum type, with one
pattern per field of that variant, in field order.
The number of fields must match what the model’s
fields reports for the variant; a mismatch
is reported as Reason::Fields.
§Examples
use match_lang::Pat;
let none = Pat::variant(0, []);
let some = Pat::variant(1, [Pat::wild()]);
assert_eq!(none.to_string(), "#0");
assert_eq!(some.to_string(), "#1(_)");Sourcepub fn product(fields: impl IntoIterator<Item = Pat>) -> Pat
pub fn product(fields: impl IntoIterator<Item = Pat>) -> Pat
The single constructor of a Product
type: a tuple, struct, record, box, or reference. One pattern per
field, in the field order the model reports; a struct pattern with
.. is written with _ for the omitted fields.
§Examples
use match_lang::Pat;
let pair = Pat::product([Pat::bool(true), Pat::wild()]);
assert_eq!(pair.to_string(), "(true, _)");Sourcepub fn range(lo: i128, hi: i128) -> Pat
pub fn range(lo: i128, hi: i128) -> Pat
An inclusive integer range lo..=hi. An exclusive range lo..hi is
range(lo, hi - 1); a half-open range takes the type’s bound for the
open end. lo > hi is reported as
Reason::EmptyRange, and a range that
leaves an Int type’s bounds as
Reason::OutOfDomain.
§Examples
use match_lang::Pat;
assert_eq!(Pat::range(0, 9).to_string(), "0..=9");Sourcepub fn char_range(lo: char, hi: char) -> Pat
pub fn char_range(lo: char, hi: char) -> Pat
An inclusive character range lo..=hi. It covers every Unicode scalar
value between the two, so a range across the surrogate block simply
skips it.
§Examples
use match_lang::Pat;
assert_eq!(Pat::char_range('a', 'z').to_string(), "'a'..='z'");Sourcepub fn lit(id: u64) -> Pat
pub fn lit(id: u64) -> Pat
An opaque literal, for an Opaque type
such as strings or floats compared by equality. Two literals are the
same value exactly when their ids are equal, so the caller interns
the literal (for example a string through intern-lang) and passes
the id.
§Examples
use match_lang::Pat;
assert_eq!(Pat::lit(3).to_string(), "lit#3");Sourcepub fn or(alternatives: impl IntoIterator<Item = Pat>) -> Pat
pub fn or(alternatives: impl IntoIterator<Item = Pat>) -> Pat
An or-pattern: matches when any alternative matches, trying them left to right. Alternatives may themselves be or-patterns. An or-pattern with no alternatives matches nothing.
§Examples
use match_lang::Pat;
let p = Pat::or([Pat::int(1), Pat::int(2)]);
assert_eq!(p.to_string(), "1 | 2");Sourcepub fn slice(elements: impl IntoIterator<Item = Pat>) -> Pat
pub fn slice(elements: impl IntoIterator<Item = Pat>) -> Pat
A fixed-length slice or array pattern [p1, ..., pn]: matches a
sequence of exactly n elements. Against an
Array type, n must equal the array’s
length (Reason::Length otherwise).
§Examples
use match_lang::Pat;
assert_eq!(Pat::slice([Pat::int(1), Pat::wild()]).to_string(), "[1, _]");Sourcepub fn slice_rest(
prefix: impl IntoIterator<Item = Pat>,
suffix: impl IntoIterator<Item = Pat>,
) -> Pat
pub fn slice_rest( prefix: impl IntoIterator<Item = Pat>, suffix: impl IntoIterator<Item = Pat>, ) -> Pat
A slice pattern with a rest, [p1, ..., pk, .., q1, ..., qm]:
matches any sequence of at least k + m elements whose first k match
prefix and whose last m match suffix.
§Examples
use match_lang::Pat;
let first_and_last = Pat::slice_rest([Pat::int(0)], [Pat::int(9)]);
assert_eq!(first_and_last.to_string(), "[0, .., 9]");
assert_eq!(Pat::slice_rest([], []).to_string(), "[..]");