mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-05-24 11:13:48 +02:00
Align smart pointer descriptions and links
We now avoid the `<T>` in the page title. We refer to `Rc` and `Arc` as reference-counted shared pointers that allow access to data from multiple places or threads.
This commit is contained in:
parent
ee1bdf39aa
commit
62f426c878
@ -140,7 +140,7 @@
|
|||||||
- [Exercise: Builder Type](memory-management/exercise.md)
|
- [Exercise: Builder Type](memory-management/exercise.md)
|
||||||
- [Solution](memory-management/solution.md)
|
- [Solution](memory-management/solution.md)
|
||||||
- [Smart Pointers](smart-pointers.md)
|
- [Smart Pointers](smart-pointers.md)
|
||||||
- [`Box<T>`](smart-pointers/box.md)
|
- [`Box`](smart-pointers/box.md)
|
||||||
- [`Rc`](smart-pointers/rc.md)
|
- [`Rc`](smart-pointers/rc.md)
|
||||||
- [Owned Trait Objects](smart-pointers/trait-objects.md)
|
- [Owned Trait Objects](smart-pointers/trait-objects.md)
|
||||||
- [Exercise: Binary Tree](smart-pointers/exercise.md)
|
- [Exercise: Binary Tree](smart-pointers/exercise.md)
|
||||||
|
@ -14,6 +14,8 @@ while still ensuring safety, typically by performing a runtime check.
|
|||||||
|
|
||||||
## `RefCell`
|
## `RefCell`
|
||||||
|
|
||||||
|
A [`RefCell<T>`] gives you mutable access to a value behind a shared reference:
|
||||||
|
|
||||||
```rust,editable
|
```rust,editable
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
|
|
||||||
@ -36,9 +38,9 @@ fn main() {
|
|||||||
|
|
||||||
## `Cell`
|
## `Cell`
|
||||||
|
|
||||||
`Cell` wraps a value and allows getting or setting the value, even with a shared
|
[`Cell<T>`] wraps a `T` value. It allows getting or setting the value, even with
|
||||||
reference to the `Cell`. However, it does not allow any references to the value.
|
a shared reference to the `Cell`. However, it does not allow any references to
|
||||||
Since there are no references, borrowing rules cannot be broken.
|
the value. Since there are no references, the borrowing rules cannot be broken.
|
||||||
|
|
||||||
```rust,editable
|
```rust,editable
|
||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
@ -72,3 +74,6 @@ that safety, and `RefCell` and `Cell` are two of them.
|
|||||||
have its own cost.
|
have its own cost.
|
||||||
|
|
||||||
</details>
|
</details>
|
||||||
|
|
||||||
|
[`Cell<T>`]: https://doc.rust-lang.org/std/cell/struct.Cell.html
|
||||||
|
[`RefCell<T>`]: https://doc.rust-lang.org/std/cell/struct.RefCell.html
|
||||||
|
@ -4,7 +4,8 @@ minutes: 5
|
|||||||
|
|
||||||
# `Arc`
|
# `Arc`
|
||||||
|
|
||||||
[`Arc<T>`][1] allows shared read-only access via `Arc::clone`:
|
[`Arc<T>`] is a reference-counted shared pointer to `T`. Use this when you need
|
||||||
|
to refer to the same data from multiple threads:
|
||||||
|
|
||||||
```rust,editable
|
```rust,editable
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
@ -26,18 +27,21 @@ fn main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
[1]: https://doc.rust-lang.org/std/sync/struct.Arc.html
|
|
||||||
|
|
||||||
<details>
|
<details>
|
||||||
|
|
||||||
- `Arc` stands for "Atomic Reference Counted", a thread safe version of `Rc`
|
- `Arc` is short for "Atomically Reference Counted". It uses atomic
|
||||||
that uses atomic operations.
|
(thread-safe) CPU instructions to maintain the reference count. `Arc` is a
|
||||||
|
thread safe version of [`Rc`].
|
||||||
- `Arc<T>` implements `Clone` whether or not `T` does. It implements `Send` and
|
- `Arc<T>` implements `Clone` whether or not `T` does. It implements `Send` and
|
||||||
`Sync` if and only if `T` implements them both.
|
`Sync` if and only if `T` implements them both.
|
||||||
- `Arc::clone()` has the cost of atomic operations that get executed, but after
|
- `Arc::clone()` has the cost of atomic operations that get executed, but after
|
||||||
that the use of the `T` is free.
|
that the use of the `T` is free.
|
||||||
- Beware of reference cycles, `Arc` does not use a garbage collector to detect
|
- Beware of reference cycles, `Arc` does not use a garbage collector to detect
|
||||||
them.
|
them.
|
||||||
- `std::sync::Weak` can help.
|
- [`std::sync::Weak`] can help avoid reference cycles.
|
||||||
|
|
||||||
</details>
|
</details>
|
||||||
|
|
||||||
|
[`Arc<T>`]: https://doc.rust-lang.org/std/sync/struct.Arc.html
|
||||||
|
[`Rc`]: ../../smart-pointers/rc.md
|
||||||
|
[`std::sync::Weak`]: https://doc.rust-lang.org/std/sync/struct.Weak.html
|
||||||
|
@ -6,7 +6,7 @@ minutes: 14
|
|||||||
|
|
||||||
[`Mutex<T>`][1] ensures mutual exclusion _and_ allows mutable access to `T`
|
[`Mutex<T>`][1] ensures mutual exclusion _and_ allows mutable access to `T`
|
||||||
behind a read-only interface (another form of
|
behind a read-only interface (another form of
|
||||||
[interior mutability](../../borrowing/interior-mutability)):
|
[interior mutability](../../borrowing/interior-mutability.md)):
|
||||||
|
|
||||||
```rust,editable
|
```rust,editable
|
||||||
use std::sync::Mutex;
|
use std::sync::Mutex;
|
||||||
|
@ -2,10 +2,10 @@
|
|||||||
minutes: 8
|
minutes: 8
|
||||||
---
|
---
|
||||||
|
|
||||||
# `Box<T>`
|
# `Box`
|
||||||
|
|
||||||
[`Box`](https://doc.rust-lang.org/std/boxed/struct.Box.html) is an owned pointer
|
[`Box<T>`](https://doc.rust-lang.org/std/boxed/struct.Box.html) is an owned
|
||||||
to data on the heap:
|
pointer to a `T` on the heap:
|
||||||
|
|
||||||
```rust,editable
|
```rust,editable
|
||||||
fn main() {
|
fn main() {
|
||||||
@ -28,8 +28,7 @@ fn main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
`Box<T>` implements `Deref<Target = T>`, which means that you can
|
`Box<T>` implements `Deref<Target = T>`, which means that you can
|
||||||
[call methods
|
[call methods from `T` directly](https://doc.rust-lang.org/std/ops/trait.Deref.html#more-on-deref-coercion).
|
||||||
from `T` directly on a `Box<T>`](https://doc.rust-lang.org/std/ops/trait.Deref.html#more-on-deref-coercion).
|
|
||||||
|
|
||||||
Recursive data types or data types with dynamic sizes need to use a `Box`:
|
Recursive data types or data types with dynamic sizes need to use a `Box`:
|
||||||
|
|
||||||
|
@ -4,8 +4,8 @@ minutes: 5
|
|||||||
|
|
||||||
# `Rc`
|
# `Rc`
|
||||||
|
|
||||||
[`Rc`][1] is a reference-counted shared pointer. Use this when you need to refer
|
[`Rc<T>`][1] is a reference-counted shared pointer to `T`. Use this when you
|
||||||
to the same data from multiple places:
|
need to refer to the same data from multiple places:
|
||||||
|
|
||||||
```rust,editable
|
```rust,editable
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
@ -25,7 +25,7 @@ fn main() {
|
|||||||
|
|
||||||
[1]: https://doc.rust-lang.org/std/rc/struct.Rc.html
|
[1]: https://doc.rust-lang.org/std/rc/struct.Rc.html
|
||||||
[2]: ../concurrency/shared-state/arc.md
|
[2]: ../concurrency/shared-state/arc.md
|
||||||
[3]: https://doc.rust-lang.org/std/sync/struct.Mutex.html
|
[3]: ../concurrency/shared-state/mutex.md
|
||||||
[4]: https://doc.rust-lang.org/std/rc/struct.Weak.html
|
[4]: https://doc.rust-lang.org/std/rc/struct.Weak.html
|
||||||
|
|
||||||
<details>
|
<details>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user