`Rc`
Rc<T> is a single-threaded reference-counted pointer. clone gives another owner of the same T. The last Rc drops the T. It is not Send.
Detailed Explanation
Sometimes one owner is a lie. A GUI widget tree, a graph of interned strings, a cache that several functions hold: they all want the same allocation. Rc::new(data) creates it. Rc::clone(&rc) increments a counter. When the counter hits zero, T drops. Cloning is O(1). It is not a deep copy.
Rc is not thread-safe. The counter is a plain integer. Sending an Rc to another thread is a compile error. That is Arc's job. Using Rc in async tasks that might move across Tokio workers is the same error. Stay on one thread, or switch to Arc.
Rc<T> gives &T. Mutation needs Rc<RefCell<T>>, the standard single-thread shared-mutability pair. Cycles leak: two Rcs pointing at each other never hit zero. Weak breaks the cycle; upgrade fails if the T is already gone.
get_mut yields &mut T only when the count is one. make_mut clones the inner value if the count is greater than one (clone-on-write). Those are the escape hatches when you thought you shared and it turns out you need uniqueness.
When you reach for Rc: single-thread shared ownership. When you reach for Arc: threads or Send tasks. When you reach for Box: one owner is enough.
Prefer Rc over clone of a large immutable tree on one thread. Prefer owning the value if only one site needs it. Rc is a tool for shared structure, not a way to avoid thinking about who owns the graph. If the graph is a tree with a clear root, store children in Vec<Node> and skip the counts.
Code Examples
use std::rc::Rc;
fn main() {
let a = Rc::new(String::from("ada"));
let b = Rc::clone(&a);
println!("{} {}", a, b); // prints: ada ada
println!("{}", Rc::strong_count(&a)); // prints: 2
}Common Gotchas
`Rc` is not `Send`
The compiler is protecting the non-atomic counter. Use `Arc` across threads.
Cycles leak
Parent/child graphs need `Weak` on one edge.
`Rc<RefCell<T>>` can panic on a double borrow
The sharing is safe for memory; the borrow rules move to runtime.