Re:Rust for JavaScript Developers – Pattern Matching and Enums
by estebank on Sunday July 12, 2020 @12:54PM
(#23812734)
You will find pre-RFCs[1][2] in https://internals.rust-lang.org/ about this, as well as for anonymous structs[3].
It isn't in the language yet due to a combination of subtle issues that need to be considered before a final design can be reached, trying to keep the grammar clean, accepting that explicitness and verboseness now is better than hastily moving towards something that seems better but that can introduce technical, documentation or understandability issues.
> Is this because destructuring syntax is trying to be compatible with the pattern matching syntax?
It's not trying to be compatible, it is pattern matching syntax. If the language changed to, for example, allow the following
let _ { name, city } = person;
Would also allow match opt_person {
Some(_ { name, city }) => {}
None => {}
}
[1]: https://internals.rust-lang.org/t/pre-rfc-struct-constructor...[2]: https://internals.rust-lang.org/t/pre-rfc-implied-type-for-p...
[3]: https://internals.rust-lang.org/t/pre-rfc-anonymous-struct-a...
Re:Rust for JavaScript Developers – Pattern Matching and Enums
by steveklabnik on Sunday July 12, 2020 @12:49PM
(#23812690)
Not the author, but yes, this is because the thing between "let" and "=" is a pattern, the exact same thing used in match, and in other places. For example, function arguments!
fn takes_person(p: Person) {
fn takes_person(Person {name, city}: Person) {
But I wouldn't inherently say that's the reason, it's also because structs are nominally rather than structurally typed.
Why does Rust destructuring require the struct name? The example uses:
This seems a little verbose since I would expect the type system to know that person is of type Person already.Is this because destructuring syntax is trying to be compatible with the pattern matching syntax?