Data-Oriented Programming in Rust

I don't think so. Note that topicstarter talks about lodash. That's Javascript library. Which explains a lot of things.

There are no immutable functions and, more importantly, there are no need to have immutable functions.

The deal here is that Rust represent the next logical step after data-oriented programming.

The problem that data oriented programming tries to fix is shared mutable data: all these stubborn and hard to fix errors which happen when you modify some data in one part of your program without properly changing some other data in another part of your program.

Data-oriented programming solves that issue with very heavy hammer: it just makes shared mutable data impossible because it makes mutable data impossible. That's the same idea which lies in the basement of other functional languages, too.

It's safe style but not very efficient: very often you need to copy a lot of data when you are doing very tiny modification.

Rust does half-step back: the central idea of Rust is that you don't need to forbid mutable data to make mutable shared data impossible. If you can, somehow, ensure that you can only modify data when nobody else may look on it you get the same benefits (two different parts of programs can not disagree about what they are observing if there are no such parts and if there are only one owner of data), but it's more efficient (and often easier to read and write, too).

You can not meaningfully employ this style in Javascript because ensuring that there are only ever one owner of some data without compiler help is very error-prone.

But in Rust it's just most natural style! The whole language is built around that idiom! Just use it and you would get most of benefits of data-oriented programming without even need to think about that term.

But there are one important exception: interior mutability. If you use that one then you get most (although not most) problems of shared mutable state. But it's hard to employ that by accident: compiler would never make anything internally mutable implicitly, all these constructs are very explicit.

P.S. As for lodash… I don't think there are anything like that for Rust. Because most common operations are, indeed, built into standard library and there are many crates which provide extensions for it, but they are, usually, small, focused, crates, not large ones which include a lot of tools, like lodash.

10 Likes