07 · Pattern Matching
Typed string captures
A pat literal matches the entire string and converts its captures before exposing bindings. A failed scalar conversion or validated constructor is a non-match. Matching uses the first delimiter without backtracking; adjacent captures without a delimiter are rejected.
On this page
A pat literal matches the entire string and converts its captures before exposing bindings. A failed scalar conversion or validated constructor is a non-match. Matching uses the first delimiter without backtracking; adjacent captures without a delimiter are rejected.
Complete example
Typed string captures.
17-string-captures.zolo
// Complete-string matching; conversions and guards can fall through.
fn order(input: str) -> str {
match input {
pat"order/{id: int}/{status: str}" if id > 0 => "{id}:{status}",
_ => "invalid",
}
}
print(order("order/42/paid")) // expected: 42:paid
print(order("order/oops/paid")) // expected: invalid
print(order("prefixorder/7/paid")) // expected: invalid
print(order("order/-1/paid")) // expected: invalid
let enabled = match "flag/true" {
pat"flag/{value: bool}" => value,
_ => false,
}
print(enabled) // expected: true
let braces = match r"{done}/7" {
pat"{{done}}/{value: int}" => value,
_ => -1,
}
print(braces) // expected: 7
newtype Positive(int) where |value| value > 0
let checked = match "id/23" {
pat"id/{value: Positive}" => value.unwrap(),
_ => -1,
}
print(checked) // expected: 23
let rejected = match "id/-2" {
pat"id/{value: Positive}" => value.unwrap(),
_ => -1,
}
print(rejected) // expected: -1
Rules and behavior
Validation, schemas and units · Documentation
From a checkout of the repository:
zolo examples/features/10-pattern-matching/17-string-captures.zolo
See also