It's a concise way to unpack Structs and Messages into distinct variables, which mirrors the instantiation syntax:
// Definition of Example
struct Example { number: Int }
fun basic() {
// Basic syntax of destructuring assignment
// is shown on the left of "=":
let Example { number } =
Example { number: 42 };
// The syntax above is roughly equivalent
// to the following statements:
let example = Example { number: 42 };
let number = example.number;
}
One can also create bindings under different variable names or ignore some of the fields. Note, that the order doesn't matter:
// "first" goes first, then goes "second"
struct Two { first: Int; second: String }
// a helper function
fun makeTwo(): Two {
return Two {
first: 42,
second: "yee-haw",
};
}
// 👀
fun totalDestruction() {
// The "first" field is ignored,
// while the value of "second"
// is bound under the "cowboy"
// variable from now on:
let Two {
second: cowboy,
first: _,
} = makeTwo();
}
🧑🍳 Contributors: Gusarich, Novus Nota (docs)
🐙 Implementation: #856
🍽 To be released in: v1.6.0
🍲 everyone within a 100 mile radius after you eat a taco
♨️ @tact_kitchen
