Skip to content

pointer guide, rc and arc #16756

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 44 additions & 2 deletions src/doc/guide-pointers.md
Original file line number Diff line number Diff line change
Expand Up @@ -625,11 +625,53 @@ See [Returning Pointers](#returning-pointers) below for more.

# Rc and Arc

This part is coming soon.
The `Rc<T>` and `Arc<T>` types are related, but not identical. They are useful
for having multiple owners for a bit of immutable data.

They get their names from **reference counted**, which is a name for the
strategy they use to deal with multiple owners. "Reference counting" is a
method where you count the number of times something is referenced. When it
drops to zero, you know that you can deallocate a resource.

`Rc<T>` is named 'arr cee' after the first two letters of 'reference counting.'
`Arc<T>` (said like 'ark') provides an 'atomic reference count'ed type. The
difference is that `Arc<T>` is threadsafe, but more expensive when updating the
count. The types are otherwise identical, and so we'll just talk about `Arc<T>`
for the rest of this section.

You can use the `clone()` method to get an additional reference to an `Arc<T>`.
This method hands out another reference to its data, and then increments the
count. When each reference goes out of scope, it decreases the count, and if
the count is zero, it frees the resource.

As an example:

```{rust}
fn foo () {
let x = Arc::new(5i); // `x` has a count of 1.

{
let y = x.clone(); // `x` has a count of 2.

{
let z = x.clone() // `x` has a count of 3.
} // `x` has a count of 2
} // `x` has a count of 1
} // `x` has a count of 0, and is destroyed
```

The `Arc<T>` type doesn't handle circular references, though: if you have
multiple `Arc<T>`s that refer to each other, their counts will always stay at
one, even if nothing points to them, and you'll leak memory. In that case, the
`Weak<T>` type can create a **weak pointer** to handle this case. If a group of
references are all weak, then they can all be freed.

## Best practices

This part is coming soon.
Use `Rc<T>` when you don't need thread safety, and `Arc<T>` when you do.

If you may introduce a reference cycle, consider adding a `Weak<T>` so that you
don't leak memory.

# Gc

Expand Down