by sesm · 1y ago · 0 comments
All the core decisions are the same as in C++:
- RAII
- Lock-based concurrency
- In-place mutation
People get confused when they hear that Rust is 'related to ML' and has Refs, but Rust Refs have nothing to do with ML Refs: ML Refs are mutable pointers to immutable data, Rust Refs are pointers to data mutated in-place.
Reference counting is not 'Rust memory management' it's a well-known technique with well-known problems, and Rust doesn't bring anything new to the table here.
Edit: RIAA -> RAII
While this may work as a rough heuristic, it's not true in theory nor in practice. First example that comes to mind: Rust has proper sum types (Called enums), which are used everywhere, and C++ doesn't have anything like it.
In C++ everything is just more awkward since everything is bolted on as an afterthought in the stdlib but then lacking language syntax sugar to make its usage convenient.
E.g. the (somewhat weird looking) equivalent to Rust's match is called 'visit' in C++:
I'm sure you could technically emulate all this in C++ is you keep throwing template metaprogramming at it, but at that point anything is a C++ subset and the statement loses all pratical meaning.
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/p26...
By the way, ever noticed that after C89, neither Ritchie nor Dennis ever had anything else to do with how WG14, or the compiler industry vendors, decided to take C onwards?
The original Plan 9 C compiler isn't even standards compliant.
Jokes aside, I think the biggest difference between Rust and C++ lies in metaprogramming and generics. Rust macros are a trip.
Rust's sum types and traits are also much more deeply integrated into the syntax of the language than std::variant or concepts.
Not at all. In a Rust program, all objects are supposed to be movable via a memcpy (i.e. a bitwise copy) by default. Immovable objects are only supported as an afterthought, via the Pin<> featureset (which is widely regarded as unintuitive, clunky and in need of revision). C++ objects are immovable by default; alternately, they may support custom "move" operators that are closer to the way .take() is used in Rust, i.e. they leave behind the original object in some sort of undetermined state.
These are quite significant differences that make it hard to have consistent interop across both languages.
But what happens to inbound references when I'm moving my object? Who is in charge of updating them?
This is like saying that Julia is a subset of Python with JIT compilation. Rust and C++ have some conceptual similarities, but they are entirely different languages.