Hacker News new | ask | show | jobs
by bad_user 843 days ago
What "immutable" means in Rust is very different from Java or any FP language for thar matter.

In Rust you can take any data structure and mutate it. There are hardly any invariants that you can force, unlike in Java. For example, strings are mutable in Rust. This, coupled with not being a language managed by GC, makes persistent data structures not practical or desirable.

Of course, mutability in Rust is very controlled, as variables being mutated can't be observed in an unsafe way. So it matters less if strings are mutable. E.g., you won't be able to mutate a string while it's used as a key in a hashmap.

But the fact is that, while Rust provides a high degree of safety, functions in Rust are usually side effecting, which means no referential transparency, no equational reasoning, hard to refactor, etc.

In Java, culturally speaking, you don't see much immutability, but when you have immutability, it tends to matter much like in FP languages. And Rust is definitely not one of those languages.

3 comments

Mutation in Rust is much more controlled than in SML or OCaml. Any record or variable (sic!) can contain a ref cell, or a special data type like an array, which can be mutated regardless of the type of the thing it’s contained within. That’s the ML approach to mutation.

In Rust on the other hand, even though ref cells are a thing as well, the main approach to mutation is through mutable bindings and mutable references, which preserve const-correctness (inspired by C++), i.e. there is a certain degree of transitivity in the guarantees around immutability. When I see an immutable reference in Rust and pass it to a function (or a method), then, outside of few special cases, I can reasonably expect it to stay unmodified. In ML I can’t, unless the code is written in a very unoptimal way, i.e. with persistent data structures anywhere and everywhere, and still the type system won’t give me any hints about that.

> In Java, culturally speaking, you don't see much immutability, but when you have immutability

This isn't strictly true. Almost all Java code I see day to day are immutable classes outside of those being used with JPA.

> In Rust you can take any data structure and mutate it.

What if a memory location not marked "mut" is allocated by the compiler in the executable TEXT segment, so the virtual memory page is itself mapped read only?

Are you certain you can get away with what you said, within the promises of the compiler?

The underlying object type would still be mutable in this case, you just can't get a &mut reference to the object.
You get a segmentation violation if you try writing to a read-only page as in variables in a .text segment.

Are we talking about different things? I'm writing Rust too (and decades of C) and feel I'm missing what you're describing, perhaps.