2 ms·
Can you please point me to the examples of the kind of the pattern matching constructs that don't allow using := ? I know it's obvious to you, I don't have your
by acqq 13y ago
Can you please point me to the examples of the kind of the pattern matching constructs that don't allow using := ? I know it's obvious to you, I don't have your experience but I'd really want to understand. Thanks in advance.
Regarding "mut is not a part of a type," so what? From my perspective (of somebody who implemented just much simpler languages than Rust) it's something like a "storage attribute." It doesn't prevent me to build the syntax to be more readable: it's a keyword anyway. It's certainly not that it can't be there because otherwise it's unparsable.
Having x = expr, x := expr and x : mut = expr make all pretty clean and obvious to me...
- acqq 13y agoI've tried to find any meaningful example of "why not." Is "let" supposed to be needed because of something like this? let (a, b) = get_tuple_of_two_ints(); I don't see that it's fundamentally different to parse if it would be written like this: (a, b) := get_tuple_of_two_ints(); It's true that the parser has to wait for := to do know the semantics of the expression on the left side of it, but it can definitely be done.
- bjz_ 13y agoRust can pattern match on structs, fixed length vectors, references, and it has keywords to bind them as mutable and/or by-ref. Doing all that without a `let` would extremely challenging.
- pcwalton 13y agoIt would require making "expression" and "pattern" into the same nonterminal in the grammar, and then deciding afterwards whether something that was parsed was an expression or a pattern. This is sometimes called a "cover grammar" because it "covers" two distinct nonterminals. It is possible--ECMAScript 6 does it--but it's definitely a complex way to parse. It doesn't seem compatible with Rust's design goal of being easy to parse and regular, e.g. for tooling.
- bjz_ 13y agoI would check out: http://pzol.github.io/getting_rusty/posts/20140417_destructuring_in_rust/ http://pzol.github.io/getting_rusty/posts/20140417_destructu... For example: struct Foo { x: (uint, uint), y: uint } let foo = Foo { x: (1, 2), y: 3 }; let Foo { x: tuple @ (a, b), .. } = foo; // => a == 1; b == 2; tuple == (1, 2) In terms of mut: let (mut x, ref y, &z) = (1, 2, &3); x = 2; // reassignment is ok, because we bound `x` as mutable x += *y; // we bound `y` as a reference, so it must be derefed first x += z; // we used a `&` pattern to get the value stored in the reference, so // derefing is not necessary *y += 1; // this would not compile, because y is immutable
- acqq 13y agoThanks. Now I see that my preferred approach would collide with current declaration logic of having mut ref and & in front of the names. Otherwise it would seem possible foo := Foo { x: (1, 2), y: 3 }; Foo { x: tuple @ (a, b), .. } := foo; ( x: mut, y: ref, &z ) := (1, 2, &3); xx: mut := 33; I see that having "x: mut" has one character more than "mut x" but I don't believe it makes the text less readable, quite the contrary because it's certainly less surprising when everything that describes the variable follows the ":" Thanks a lot, I've certainly learned more about Rust this way.
- bjz_ 13y agoShowing let bindings with their explicit type declarations might help: let mut x: int = 2; let x: &mut int = &mut 2; let (mut x, ref y, &z): (int, int, &int) = (1, 2, &3); The pattern binding goes to the left of the `:`, and the input type goes to the right. Also note that you can pattern match on types too: let (mut x, ref y, &z): (_, int, &_) = (1, 2, &3);
- acqq 13y agoThanks, if _ is allowed as the type, why are explicit types written at all? Is the last example equivalent to let (mut x, ref y, &z): (_, _, &_) = (1, 2, &3); and can we go even further let (mut x, ref y, &z) = (1, 2, &3); If we can, when do we actually need specifying the types? To do the type conversion or what? Probably because I don't know Haskell (sorry) but I still imagine x y and z as names and even mut, ref and & as the "access" specifications, which define somehow the "type" of x y and z and not the type the thing accessed with the given name.