4 ms·
I never got the point of having borrow checker in a single-threaded environment. Can someplace please explain its usefulness to me in that scenario?
by viktorcode 4y ago
I never got the point of having borrow checker in a single-threaded environment. Can someplace please explain its usefulness to me in that scenario?
- nickez 4y agoYou avoid use after free for example
- TheDong 4y agoLet me show you a bug in Go which involves no threads/goroutines, but would be solved by the borrow checker: https://go.dev/play/p/sEptCgMM0Bh https://go.dev/play/p/sEptCgMM0Bh In rust, you could not write that code without using either unsafe, or getting borrow checker errors.
- Twisol 4y agoThe borrow checker statically prevents having mutation and aliasing at the same time. Aliasing is when two names refer to the same state; mutable aliases make it possible for "someone else" to act on that state while you're using it. Consider the humble for-each loop: for (let x : xs) { xs.remove(0); } If you're not careful, modifying a collection while you're traversing it can cause you to visit elements multiple times, or skip some elements entirely. There are effectively two tasks occurring at the same time -- interleaved, not parallel, but still concurrent: the ordered traversal and the action on each element. Both tasks can view the list, but one of them also modifies it behind the other's back. This problem simply can't happen in Rust (without going out of your way, at least), because you can't independently mutate a list you're already iterating over.