Rc<T> enables shared ownership by tracking reference counts to free data only when all owners are dropped.
Rc enables shared ownership by wrapping a value in a reference-counted smart pointer that tracks how many owners exist. When you clone an Rc, the reference count increments; when an Rc is dropped, the count decrements, and the data is freed only when the count reaches zero.
use std::rc::Rc;
fn main() {
let data = Rc::new(String::from("shared"));
let clone = Rc::clone(&data);
println!("Count: {}", Rc::strong_count(&data)); // Prints 2
}
Rc lets multiple parts of your program own the same data without copying it. It works like a shared document where a counter tracks how many people have a copy; the document is only deleted when everyone closes their copy. You use this when you need to pass data between different parts of your code that all need to keep it alive.