Skip to content

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
Playground
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

Reference · Source program

zolo run examples/features/40-syntax-fourth/08-pin-patterns.zolo --no-cache

Global index

Find your way through Zolo

Try an idea

Start here

9 results

9 results

enespt-br