Skip to content

07 · Pattern Matching

Read nested fields in a pattern

Write { address.city: city } to bind a nested field without repeating record braces. The full path is checked against the input type; the local city binding belongs to the successful pattern scope.

On this page

Write { address.city: city } to bind a nested field without repeating record braces. The full path is checked against the input type; the local city binding belongs to the successful pattern scope.

Complete example

Read nested fields in a pattern.

07-nested-destructure.zolo
Playground
struct Address { city: str, postcode: int }
struct Profile { name: str }
struct User { address: Address, profile: Profile }

let user = User {
    address: Address { city: "Recife", postcode: 50000 },
    profile: Profile { name: "Ada" }
}
fn mailing_label(user: User) -> str {
    let { address.city: city, address.postcode: postcode, profile.name: name } = user
    assert(city == "Recife")
    assert(postcode == 50000)
    assert(name == "Ada")
    "{name}: {city}, {postcode}"
}
print(mailing_label(user))

Reference

Reference · Source program

zolo run examples/features/40-syntax-fourth/07-nested-destructure.zolo --no-cache

Global index

Find your way through Zolo

Try an idea

Start here

9 results

9 results

enespt-br