Var as alias for let mut

Hello,

It seems to me that most of your arguments are not in line with the Rust language design philosophy:

  • Rust vision of productivity is not only about typing your code faster but also about having your code correct faster. Rust try to promote the use of immutability whenever possible because it has nice properties about safety. Declaring a mutable variable requires a few more keystrokes on purpose, to prevent from using mutable everywhere by convenience.

  • Perl is a famous example of a language that is complex to handle because of too many ways to write the same thing. Rust prefer to prevent two syntax to do the same thing when it is possible to keep the language simple. For instance the ternary operator was removed because the if block can do exacty the same. The return keyword is necessary to do early return. If early return was not allowed by the language, the return keyword would probably not exist.

  • const and let are very different concepts in Rust. const variable are guarantied to be evaluated at compile time. While let variable are just guarantied to not be modified once there are initialized.

var is a pretty common keyword for variable declaration, but not an ubiquitous one by the way. C++ use auto for instance.

4 Likes

It should also be pointed out that a lot of the feedback about productivity in Rust is very positive in Rust’s favor. And the property of immutability by default is one that prevents a lot of unnecessary debug cycles, increasing overall productivity.

3 Likes

One last time: Rust should learn from languages such as Java and C# and evolve it’s language and syntax as much as possible. Java and C# are all always evolving and adding feature, these languages aren’t top 5 languages for nothing.

I challenge that assertion. To me, Java is an incredibly slow moving language that only adds features when it has been in other languages forever. On the contrary, Rust is to me the language that has an incredible design and implementation pace. I don't think there's any compiler project that is so active as the rustc repository is. Meanwhile, we are accepting new RFCs more or less every other week.

9 Likes

That’s great!

Orthogonally to the matter of var, if we had scoped type variables which are quite nice in Haskell (and for other things), I think const inside functions could feel quite close to let in spirit; The former being compile time bindings and the latter being runtime bindings. It would be nice seeing some movement in that direction.

Careful. let mut is not the unit! mut name is a pattern, and then you can let-bind that pattern with let mut name, which you should think of as let (mut name). This is why you have let (mut foo, mut bar) = pair; right now.

5 Likes

I was simplifying for comparison’s sake, but you’re right of course.

1 Like

It isn’t bad. But having multiple ways often confuse people

Actually, I believe that it is bad, because it is based on an incorrect parse of the Rust grammar, thus leading to faulty perception. In fact, the title of this topic demonstrates just such an incorrect parse by the topic's originator (and one which I used to make myself :roll_eyes:).

As @Centril pointed out

The lesson I've learned from this discussion is that Rust's documentation should emphasize @Centril's point: that let is the declaration and mut, if present, is an attribute of the identifier being declared. Per @Centril's example, slightly modified, it is possible to write

let (foo, mut bar) = pair;

which clearly demonstrates that the proposed var syntax is not an alias.

8 Likes

Me too. But I didn't want to sound too negative :grin:

2 Likes

Yeah, they sometimes are.

14 Likes

you may also like this reference along the same lines:

Monotony of design - there should be only one way to accomplish a certain atomic task in an application [or language in this case]

from: The Humane Interface - Wikipedia

3 Likes

I strongly oppose this request because it would only clutter the language without any benefits; actually, it would be a step backwards in terms of reliability and correctness. However, it also seems to me that you suffer from a serious lack of understanding of Rust's syntax, semantics, and overall goals/roadmap. So let me address that first and break down what's wrong with your arguments:

var keyword as an alias for let mut

let mut is not one keyword/syntactic construct, but two. It's a let binding with a mut pattern. In other words, it's a composition of two syntactic elements. You can do let Foo { field_1, mut field2 } = some_struct; in a struct, for example, or if let Some(value) = an_optional { … }. Thus, it doesn't make sense to talk about "an alias of let mut", because you can't alias it, it's simply not a thing. In other words, var wouldn't compose, it would be a very niche addition for one particular constellation of let and mut, which would work in only one case: when the let and mut tokens happen to follow each other. This would be a serious violation of the decomposability of Rust's concrete syntax into a tree, because it is no longer about syntactic structure, but approximately plaintext search-and-replace.

I think people who would like to create mutable variables by default without much cognitive thinking deserve that option too.

People who would like to create mutable variables by default are wrong. The last 30 years of the history of computer science and software engineering proved that immutability is one of the best and easiest ways to achieve reliability, and that for this reason, immutable-by-default is the right choice when designing a programming language. I'm not a purist or an extremist: I don't like pure functional languages that don't allow mutable variables at all. Sometimes, they are necessary and a better solution than trying to enforce immutability in 100% of the time. However, they shouldn't be the default, simply because programs written in a mostly immutable/mostly functional style are much easier to understand and read/write correctly.

Furthermore, I simply can't believe that having to type let mut is so much of a cognitive overhead compared to var that a non-composable, special-cased, new keyword would be worth introducing to the language.

I have a feeling that you haven't been using Rust for a long time. Some Rust newcomers ask for features of languages they have been using, because they don't like Rust's explicitness at first. For example, some people complain that optionals have to be written using an explicit Some(…), but when they gain a bit of experience with Rust, they understand why it is a better way to write optionals. I think you might be in a similar situation with respect to mutability.

It would make rust more natural as a programming language similar to most languages.

Again, it's such a minor similarity or difference that it's not worth the burden of a change to the core language. let mut is not anything weird or hard to understand in particular. Of course, Rust tries to not be super weird or unfamiliar, which is a good thing. However, if the primary goal of a language was to be as similar to other languages as possible, that language might as well just not exist at all, and programmers could use one of the other language it was supposed to be similar to. Rust is not a language that has similarity to JavaScript, C#, Go, or Swift among its primary goals. (Especially in the light of two of those languages being younger than Rust.)

Making every Rust variable declaration immutable by default would greatly affect its adoption rate, it greatly reduces productivity.

No, it doesn't. Rust users are among the most productive programmers in the world, and people love it the most, as it is.

You might say that having let and var would make it confusing; however, Rust already has a similar situation with the const and let keywords, where both function as an immutable variable.

No, they don't. Neither of these two constructs are doing what you think they are doing. const is a compile-time constant, meaning that its value is known to the compiler. const is not used for immutable variables. let is also not used for immutable variables only. let is used for binding names to values, where the presence or absence of mut before a certain name in a pattern shows if that particular name will be a mutable binding.

Alright, so now that I hopefully cleared up some confusion, let's address the last bad part of var: it would be another way of writing exactly the same thing for one particular construct. Having two pieces of syntax for exactly the same semantics is very confusing. It doesn't only lack a significant advantage, it's actually actively harmful. because our monkey brains work like that. If two things look different, we expect that they be different. Similarly, if two things look the same, we expect them to behave similarly. Breaking this intuition is a very dangerous game, because it will lead to confusion, which in turn leads to bugs. It's better to spare ourselves that pain.

9 Likes

It seems that rust is a perfect language that needs no evolution or modification. Thank You guys for your inputs. Sorry for my request. I promise I won’t create any requests or discussions again.

You have your opinion and style of programming and I have mine. You can’t force or degrade the opinions of others. It’s not a dictatorship here.

Please continue to be involved! The language is not perfect and never will be. Discussions like this are the best way to drive improvements.

4 Likes

I didn’t force any particular style on anyone. Explaining why one style is factually better than another isn’t “forcing” – I didn’t point a gun at your head. Your reasoning and arguments, however, showed clear evidence of lack of experience; I attempted to explain to you why/how it is a fact that immutability helps avoid bugs. If you don’t want to program in that style, that’s up to you. However, you can’t expect the language to change in that direction either.

1 Like

You clearly didn’t force anything on me. You just dictated how a language should and must be and degraded other opinions as invalid and you clearly haven’t forced anything.

This should be a place of respectful discussion, not degrading opinions and insulting others. If you don’t like the idea, should leave a respectful comment. It’s just discussion after all, no one will reinventing rust in a day. It’s just a discussion for the record.

Also, when something was already said by other members, you don’t need to repeat it again just for the sake of creating a post of your own. Most of your arguments were already said before. If you want to add something, add something new.

2 Likes

Thank You guys for your inputs, you all have valid arguments. Hopefully this discussion will remain a reference guide for the future. I don’t actually think var is needed at this point. Any problems with writing preference or style can be solved through IDE, not requiring language change.

1 Like