Language reference · section 8
8. Destructuring
Feature: Destructuring
Scenario: An object pattern binds fields by name
Given the code "{name, age: years} = user"
Then name is user.name and years is user.age
Scenario: An array pattern binds items in order
Given the code "[first, second, ...rest] = items"
Then first is items[0], second is items[1], and rest holds the remaining items
Scenario: A rest name must be last
Given the code "[first, ...rest, last] = items"
Then the compiler reports an error "`...rest` must be the last name in a pattern"
Scenario: A pattern holds only names
Given the code "{a: 1} = obj"
Then the compiler reports an error "a pattern can only hold names, like `{name, age: years}`"
Scenario: A pattern binds only new names
Given a visible name x
When a pattern binds "x"
Then the compiler reports an error "`x` is already declared in this scope"
Scenario: mut makes every name in a pattern mutable
Given the code "mut [a, b] = xs"
Then a and b are mutable bindings
Scenario: A wildcard skips one position
Given the code "[first, _, third] = items"
Then the second item is not bound
Scenario: A pattern at the top level of a file with @wasm or @worker is an error
Given a file that uses @wasm or @worker with a top-level pattern
Then the compiler reports an error "destructuring at the top level of a file with @wasm or @worker is not supported yet; move it into a function"
Scenario: A pattern in a @wasm function is an error
Given a @wasm function with a pattern
Then the compiler reports an error "destructuring is not supported in @wasm functions"