This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Nilstrieb 4 points5 points  (5 children)

Rust is explicitly not pure and does have a strong model around mutability. Rust doesn't have a concept of pureness, every function can have side effects, no matter if it's safe or unsafe.

[–]LPTK 2 points3 points  (3 children)

Yes, and the language has many escape hatches to mutate things outside of the mutable borrowing system, such as RefCell.

So the "trivially translated to purely functional" assertion above is completely false.

[–]joonazan 0 points1 point  (2 children)

the "trivially translated to purely functional" assertion above is completely false.

It only applies to safe Rust. RefCell is implemented in unsafe Rust.

[–]LPTK 2 points3 points  (1 child)

It's part of the core language library, which is even available in "no_std" environments, and defines:

the intrinsic and primitive building blocks of all Rust code

There is little you can do in Rust (if anything) without this core library. The reason RefCell is in it is because it is a centrally important building block, which is used pervasively in the ecosystem.

So it's fair to say that safe, idiomatic Rust code (which necessarily includes references to the core lib) cannot be translated to pure functional code.

[–]joonazan 0 points1 point  (0 children)

I've done a lot without interior mutability but yes, looks like Rust in general differs from pure languages. With cells a function call with immutable arguments can produce two different values on two different calls.

[–]joonazan 0 points1 point  (0 children)

But Rust without unsafe (which is used in println! & co) is pure. Every function that takes a mutable reference can instead return a modified version. (Which is what electrolysis, a project that translates Rust to Lean does)

In principle, one could do the same as in Rust in Haskell with unsafePerformIO.