4 ms·
Rust's Arc is very similar to C++'s shared_ptr. It's a generic container type, which does reference counting on the inside. I haven't written Swift, but I beli
by oconnor663 4y ago
Rust's Arc is very similar to C++'s shared_ptr. It's a generic container type, which does reference counting on the inside.
I haven't written Swift, but I believe reference counting is mostly automatic and transparent there, more like a GC strategy than like a container.
Not sure whether anyone's to blame for the name collision :)
- zaphar 4y agoWith one very important difference. Unless you use a thread_safe interior mutability implementation like Mutex Arc won't allow you modify or change the wrapped value. C++'s shared_ptr does not enforce the same. All of which means that while the shared_ptr control block is thread safe what the shared_ptr contains may or may not be. This has caused, and will continue to cause, no end of confusion for folks doing C++ especially for less experienced devs.
- oconnor663 4y agoThere are many differences when it comes to safety: - Like you said, Rust doesn't let you data race on the contents of an Arc. - Rust also doesn't let you data race on the Arc itself (i.e. repointing a global Arc without synchronization). This is Bug #5 in this great talk: https://youtu.be/lkgszkPnV8g https://youtu.be/lkgszkPnV8g - Rust also doesn't let you retain a reference to the contents of an Arc longer than you retain the Arc. This is the bug that C++ Core Guideline R.37 is about: http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rr-smartptrget http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#... But I'm not sure I'd call these Arc vs shared_ptr differences per se, because almost any comparison between a Rust container and its C++ equivalent looks similar.