-
Notifications
You must be signed in to change notification settings - Fork 0
match
ghaerdi edited this page Aug 14, 2026
·
2 revisions
Import match and P from @ghaerdi/rustify/match.
-
match(value): Starts a match chain, returning aMatchyou extend with.with()cases and terminate with.exhaustive(),.otherwise()or.run(). -
matches(value, pattern): Standalone predicate — returnstrueifvaluematchespattern.
-
.with(pattern, handler): Adds a case.handlerreceives the value narrowed to whatpatternmatches. Returns the extended match. -
.exhaustive(): Runs the match and throws if nothing matched. At compile time, calling it on an incomplete match is a type error at the call site that names the missing cases (e.g.NeverCase<"NonExhaustive: unhandled case { type: rect }">). -
.otherwise(handler): Runs the match, callinghandler(value)for anything no case matched. -
.run(): Runs the match, returningundefinedif nothing matched — excluded from the return type when every case is covered.
Option.some, Option.none, Result.ok and Result.err match the
respective variant and pass the unwrapped value (or error) to the handler
— n below is number, not Option<number>:
match(opt)
.with(Option.some, (n) => n.toFixed(2))
.with(Option.none, () => "none")
.exhaustive();These patterns are per-variant: .with(Option.some, ...).exhaustive()
alone is a compile error naming the missing variant
(NeverCase<"NonExhaustive: unhandled case { __tag: none }">).
-
P.any/P._: Matches anything (catch-all). -
P.string,P.number,P.boolean,P.bigint,P.symbol: Matches primitive types. -
P.nullish: Matchesnullorundefined. -
P.array(pattern?): Matches arrays; optionally checks every element. -
P.instanceOf(Ctor): Matches class instances. -
P.union(...patterns): Matches any of the given patterns. -
P.when(guard): Matches when the type guard returnstrue. -
P.not(pattern): Matches everything exceptpattern. -
P.optional(pattern): Matchesundefinedorpattern.
-
Match: the chain type returned bymatch(). -
Pattern<TInput>: a valid pattern forTInput. -
Narrow<TInput, P>: the type of a value matched by patternP.