
Things I Enjoy in Rust: Immutability - ingve
https://blog.jonstodle.com/things-i-enjoy-in-rust-immutability/
======
brink
I remember when HN was infatuated with Golang, and was on the front-page of HN
what seemed like every day. Now all I see is Rust, and Golang is rarely seen.

What happened? Did HN ditch Golang for a new girlfriend?

~~~
mooman219
Go wasn't generic enough to monomorphize into the niche that rust is filling.

------
melling
I love the idea of immutability in Swift too.

let x = 42

However, for mutability I prefer when the language uses another keyword.

Swift uses 'var'. Scala and Kotlin I believe use val and var.

Throw in non-optional by default and I feel like programming becomes more
enjoyable.

------
stickfigure
Hmmmm. Being able to redeclare the same identifier in a scope makes it look
suspiciously like a variable. I like the updated_point example better.

But yeah, otherwise, awesome.

~~~
suprfnk
That was my first thought as well. If you keep mutable variables function-
scoped, I see no difference between:

    
    
        // We do this:
        let point = Point { x: 2, y: 4 };
        let point = Point { x: 4, ...point };
    

and

    
    
        let point = Point { x: 2, y: 4 };
        point.x = 4;
    

And that's generally what the problem is with mutable variables. You either
erroneously assume `x == 2`, or have to read all the code between assignment
and the point you're trying to change something.

Immutability gives you less thinking and parsing overhead. This kind of
defeats that purpose.

~~~
pixel_fcker
The difference is the let key word makes it obvious you’re rebinding the name.

Shadowing is one of those things that intuitively raises a red flag first time
you see it but actually is extremely helpful. It dramatically reduces noise
e.g. in functions where you take an input parameter and want to convert it to
a different type to work with it. No more prefixing or suffixing the variable
name, you just rebind it which makes the intent clear (at least to me).

