Persistent Data Structure Support

Rust functions and closures accept (all/most, I don't know exactly) irrefutable patterns in argument position, so whatever (?) you can write in an irrefutable pattern, you can write it in a function parameter. For instance, you can do this too:

my_iterator.enumerate().map(|(index, item)| do_stuff(index, item))
                             ^           ^
                             +-----------+

Here you don't just give a name to the tuple-typed closure argument; instead, you destructure the tuple argument in-place, and you get named access to its fields directly.

8 Likes

There's nothing wrong with local side effects, as long as they're referentially transparent.

Basically, as long as the side effects cannot be viewed outside of the function, then the function still counts as pure.

Many languages (including Haskell and Clojure) use this to dramatically speed up data structures and algorithms, by doing internal mutation.

There's many different ways of doing useful things in a pure way: monads and functional reactive programming are the most common.

But there's other less common ways too, like uniqueness types (which are actually used by Rust).

Actually, immutability can often improve performance, especially in concurrent or distributed programs.

The idea that mutability improves performance is only true on the old-fashioned (but still very popular) Von Neumann model of computing, and even that is changing (because of multi-core processors and cloud computing).

3 Likes

I am happy for the 'wow!' but sorry for the 'headache' part. But I hope all over the discussion has helped you!

It certainly has.

Never mind the headaches, they come with the intense concentration caused by finding something really interesting, trying to assimilate too much all at once! Despite the headaches I'm very taken with Rust, it has a place in my future already.

Aside:

Thanks to the rigorous checking of Rust, last week I uncovered a bunch of bugs and latent problems in the 2000 lines of C++ I was rewriting in Rust as an exercise.

That C++ was itself a translation from C#. Today Rust uncovered errors that were in the original C# and had been faithfully reproduced in C++. Rust would not let them reproduce further.

Already the headaches are paying off!

Thanks all.

7 Likes

Overall thanks to everyone for the inputs. I marked the code as a solution which was what I was trying to get to- local mutability efficiency that rust supports with safe rules instead of persistent data structures and following FP principles in general as stated in this idiomatic rust question. Cheers!

I didn't see this go by yet; apologies if this has already been posted!

Niko wrote some interesting thoughts on this topic:

http://smallcultfollowing.com/babysteps/blog/2018/02/01/in-rust-ordinary-vectors-are-values/

3 Likes

Thanks for sharing @uberjay- that is a good read and in compliance with the overall summary.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.