3 ms·
Rust doesn't need to use reference counting for shared data structures, which I'm pretty sure you already know. The use of reference counting is about dealing
by Jweb_Guru 7y ago
Rust doesn't need to use reference counting for shared data structures, which I'm pretty sure you already know. The use of reference counting is about dealing with unknown lifetimes, not dealing with sharing.
- afiori 7y agoIf there is no sharing then you just need a box, reference counting is for when there is not a clear owner and many references all still want to share. I am not sure how the thread situation is for GCs and Java, but Arc is indeed reference counting and I believe it is used quite often in rust.
- Jweb_Guru 7y agoYou can just use a shared reference to achieve sharing in Rust; you don't need a Box or an Arc. Pretty much every shared core data structure in Rust works fine with references with no Arc in sight, and this makes plenty of sense when you consider that the main way you interact with a type protected by an Arc is by extracting a shared reference from it.
- afiori 7y agoBut one of the advantages of Rc is to allow for dynamic GC-like lifetimes, where values have no clear owners.
- Jweb_Guru 7y agoYes, we are in agreement about that. But they are certainly not necessary for shared data structures (even concurrent ones) and that is what the post to which I was responding said. This also isn't a purely pedantic point; scoped threads and libraries like Rayon are explicitly designed to allow concurrent sharing of values where the sharing has a bounded lifetime, and they do not require reference counting in order to support this (indeed, some usecases of these libraries, such as splitting up arrays into disjoint slices, don't really work with reference counting).