3 ms·
Rust's ownership and borrowing rules are entirely a compile-time thing: they just determine what objects can be safely used, and when. There is absolutely no ne
by catnaroek 11y ago
Rust's ownership and borrowing rules are entirely a compile-time thing: they just determine what objects can be safely used, and when. There is absolutely no need to use dynamic allocation to benefit from Rust's ownership and borrowing checks. And none of the other core language facilities (primitive types, unsafe pointers, fixed-size arrays) require dynamic allocation either.
What does require dynamic allocation is using boxed objects and dynamically sized collections (such as vectors and tree maps), which are provided by `libstd`, but not by `libcore`. Think of using `libcore` as using C or C++ in freestanding mode.
- Peaker 11y agoAs far as I know, nobody managed to create a comprehensive intrusive data structure library that behaves as optimally as in C, due to the ownership semantic restrictions.
- catnaroek 11y agoSo just bypass the ownership checks, with `unsafe`. It gives up on memory safety, but you still keep all the other benefits of a modern language: algebraic data types, pattern matching exhaustiveness checks, parametric polymorphism, etc. So, even when writing `unsafe` code, Rust is strictly superior to C and C++. Also, I would contend that, for the vast majority of programs, the benefits of a simple, hierarchical, statically enforceable resource management scheme like RAII outweigh the downsides. Programs that need more sophisticated approaches are a very small minority.