4 ms·
Actually then remapping your brain and how you want to achieve things seems incredibly challenging The thing that I have come to appreciate more strongly is th
by danieldk 9y ago
Actually then remapping your brain and how you want to achieve things seems incredibly challenging
The thing that I have come to appreciate more strongly is that you actually have to think about the same ownership rules in other languages to write bug-free code. Consider e.g. the following Go code:
https://play.golang.org/p/3qfd5B8_ktK https://play.golang.org/p/3qfd5B8_ktK
Whether a subslice is still referring to the same array as the slice that it was derived from depends on whether there were any appends that required allocation of a larger backing array. Whether/when this happens is an implementation detail.
Rust prevents this ambiguity by not letting you borrow a `Vec` simultaneously as immutable and mutable (or multiple times mutably). Since the `push` method takes `&mut self`, you cannot have any mutable/immutable slices of the same data.
Another example. If you take a slice of a large array and only use that slice from there on, the slice will (obviously) prevent garbage collection of the backing array [1]. Again, Rust's ownership system prevents such memory leaks, since a slice reference cannot outlive its (owned) `Vec`.
[1] https://forum.golangbridge.org/t/free-memory-of-slice/3713/2 https://forum.golangbridge.org/t/free-memory-of-slice/3713/2
---
I would argue that the cognitive load in other languages is actually higher if you want to write correct code (except perhaps in languages with immutable data structures, such as Haskell), since you have now compiler helping you out. It's just that other languages allow us to cheat when it comes to ownership. For worse or better.