Rc And Arc

In Rust, Rc (short for "reference counted") and Arc (short for "atomic reference counted") are types that allow you to share values between multiple owners. They are similar to Box, which represents ownership of a value on the heap, but they allow multiple owners.

Rc is a non-thread-safe reference counted type, which means that it is not safe to share an Rc value between threads. It is useful for sharing values within a single-threaded context, such as when building a tree data structure. A value owned by an Rc pointer is immutable.

Arc is a thread-safe reference counted type, which means that it is safe to share an Arc value between threads. It is useful for sharing values between threads, because it allows multiple threads to access the value concurrently.

Rc in memory

#![allow(unused)] fn main() { use std::rc:Rc; let s : Rc<String> = Rc:new("shirataki".to_string()); let t : Rc<String> = s.clone(); let u : Rc<String> = s.clone(); }

image

Well-known problem with using reference counts

One well-known problem iwht using reference counts to manage memory is that, if there are ever two reference-counted values that point to each other, each will hold the other's reference count above zero, so the values will never be freed.

image

Example

Image

use std::rc::Rc; #[derive(Debug)] enum List<T> { Nil, Cons(T, Rc<List<T>>) } fn main() { use crate::List::{Cons, Nil}; let a = Rc::new(Cons(5, Rc::new(Cons(10, Rc::new(Nil))))); let b = Rc::new(Cons(3, Rc::clone(&a))); let c = Rc::new(Cons(4, Rc::clone(&a))); println!("{b:?}"); println!("{c:?}"); }