Conversation

I don’t know Rust, so I was wondering how this would look in C++, does anyone have an example?
Quote Tweet
Conceptually, the thing that makes Rust different from modern C++ (and other languages) is the "alias xor mutable" guideline, enforced by the borrow check. You could write C++ that way, but in practice nobody does. It's much stricter than just using smart pointers.
Show this thread
12
15
Replying to
You can have any number of immutable views (references) of data but only one mutable reference at a time. The compiler also enforces that the references can't be used beyond the lifetime of the object and the lifetime scopes are part of the types of references (as subtypes).
1
4
Mutex<T> and RwLock<T> naturally provide a dynamic implementation of those checks as part of implementing their interior mutability. Those give out objects representing locks with the destructor releasing the lock, similar to a modern C++ idiom, but safe.
1
2
Arc<T> is comparable to std::shared_ptr<T>. Rc<T> is a thread-specific variant of it without the atomic reference counting. Due to the shared ownership, those only provide immutable references. Arc<Mutex<T>> and Rc<RefCell<T>> provide a way to have shared ownership + mutability.
1
1
Show replies