You've already forked comprehensive-rust
mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-06-27 19:18:59 +02:00
23 lines
515 B
Markdown
23 lines
515 B
Markdown
![]() |
# `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
|
||
|
a type such as [`Cell` or `RefCell`][2].
|
||
|
|
||
|
[1]: https://doc.rust-lang.org/std/rc/struct.Rc.html
|
||
|
[2]: https://doc.rust-lang.org/std/cell/index.html
|