07 · Pattern Matching
Match an existing value
A ^request_id pattern compares against the outer request_id binding. It creates no new binding and cannot refer to a name introduced by the same pattern. The compared values must have compatible types.
On this page
A ^request_id pattern compares against the outer request_id binding. It creates no new binding and cannot refer to a name introduced by the same pattern. The compared values must have compatible types.
Complete example
Match an existing value.
08-pin-patterns.zolo
struct Reply { request_id: int, data: str }
let request_id = 42
let reply = Reply { request_id: 42, data: "accepted" }
let other = Reply { request_id: 99, data: "stale" }
let accepted = match reply {
{ request_id: ^request_id, data, .. } => data,
_ => "ignored"
}
let ignored = match other {
{ request_id: ^request_id, data, .. } => data,
_ => "ignored"
}
assert(accepted == "accepted")
assert(ignored == "ignored")
assert(request_id == 42)
print(accepted)
print(ignored)
Reference
zolo run examples/features/40-syntax-fourth/08-pin-patterns.zolo --no-cache
See also