Reference counting is a simple form of garbage collection. The basic flow of events with Rc is as follows:
- Every time somebody takes a new reference, we increment an internal counter
- Every time somebody drops a reference, we decrement it
- When the internal counter hits zero, nobody refers to the object anymore, so it can be dropped
Using variables in reference counted containers gives us more flexibility in the implementation: we can hand out references to a value without having to keep exact track of when the references go out of scope.
Rc<T> is mostly used via two methods:
- The static method Rc::new makes a new reference collected container (you should start recognizing a pattern already!)