3 ms·
Incidentally, I don’t know why I can write let (mut word, mut len, mut ones) = (0u32, 0, 0); but not (word, len, ones) = (0, 0, 0); Marki
by ufo 11y ago
Incidentally, I don’t know why I can write
let (mut word, mut len, mut ones) = (0u32, 0, 0);
but not
(word, len, ones) = (0, 0, 0);
Marking variable declarations with let is a good thing and makes it clear if something is an assignment or a variable declaration. It also leads to cleaner code when you use nested functions (I always hated the "nonlocal" keyword in Python)
- losvedir 11y agoI think the author was indicating that while you can use that syntax to declare and initialize variables, you can't use it later to assign new values to them. I don't think they had any problems with the concept of `let`. And it does strike me as a little weird as well to only be able to take advantage of the pattern match destructuring on initialization, but I'm sure there's a good technical reason.
- deng 11y agoI read it the same way. There's an open issue for it: https://github.com/rust-lang/rfcs/issues/372 https://github.com/rust-lang/rfcs/issues/372 I think one of the main reasons this isn't done is because of the grammar issue. I dimly remember that one of the goals of Rust was to be parseable with a LL(1) grammar.
- steveklabnik 11y agoThere are grammar issues, yes. We unfortunately have one tiny case that's context-sensitive though, so we didn't quite get there :/ that vast majority of the grammar is very straightforward though. There's a lot of benefit to it.
- masklinn 11y agoThe main reason this isn't done is that `let (a, b) = (…)` is the pattern matching of two tuples, but assignment doesn't do pattern-matching.
- sremani 11y agolet (word, len, ones) = (0, 0, 0) looks like a cleaner version that works in F# (and I am not sure in Rust). This is extremely useful when initializing values from a function that returns a tuple and do not want to change them (unlike the mut word etc.) let (x, y, z) = GetStartPoint3d()
- steveklabnik 11y agoThat works in Rust if you add a ; on the end.
- sremani 11y agoNice. The original article does an interesting job, but what truly blows my mind is Rust gives systems level of performance while supporting highorder functions and aspects of functional programming. That is truly remarkable. On the side note, adding the ; on the end also works in F# but is not idiomatic.
- steveklabnik 11y agoYeah, the optimizations are pretty great. I really love our closures too, it enables all kinds of good stuff. (And thanks for the idiom note, makes sense. F# is pretty cool.)