1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-06-27 19:18:59 +02:00
Files
comprehensive-rust/src/std/rc.md

33 lines
872 B
Markdown
Raw Normal View History

2022-12-21 16:36:30 +01:00
# `Rc`
[`Rc`][1] is a reference-counted shared pointer. Use this when you need to refer
to the same data from multiple places:
```rust,editable
use std::rc::Rc;
fn main() {
let mut a = Rc::new(10);
let mut b = a.clone();
println!("a: {a}");
println!("b: {b}");
}
```
If you need to mutate the data inside an `Rc`, you will need to wrap the data in
2022-12-28 10:15:17 +01:00
a type such as [`Cell` or `RefCell`][2]. See [`Arc`][3] if you are in a multi-threaded
context.
2022-12-21 16:36:30 +01:00
[1]: https://doc.rust-lang.org/std/rc/struct.Rc.html
[2]: https://doc.rust-lang.org/std/cell/index.html
2022-12-28 10:15:17 +01:00
[3]: ../concurrency/shared_state/arc.md
2023-01-11 14:04:30 +01:00
<details>
* Like C++'s `std::shared_ptr`.
* `clone` is cheap: creates a pointer to the same allocation and increases the reference count.
* `make_mut` actually clones the inner value if necessary ("clone-on-write") and returns a mutable reference.
</details>