2 ms·
Both Swift and Rust use reference counting in different ways. Swift uses reference counting for all reference types (classes), but idiomatic Swift encourages u
by millstone 10y ago
Both Swift and Rust use reference counting in different ways.
Swift uses reference counting for all reference types (classes), but idiomatic Swift encourages using value types over reference types when possible. The chief reason to use a reference type is when you need shared ownership. Value types are not reference counted, though they may contain RC'd fields.
Rust has reference counting implemented in a library (Rc and Arc), which is what Rust programmers reach for when they need shared ownership. Rust also has a 1-bit reference count called the drop flag, which is used to dynamically determine when an object's destructor should run.
- pcwalton 10y agoThe drop flag is not a reference count. It is an implementation detail, and we could eliminate it if we wanted to. Swift's value types are less flexible than those of Rust because of the need to maintain soundness in the presence of aliasing and mutation.