Use Rc<T> to enable shared ownership of data in single-threaded Rust code by tracking reference counts.
Use Rc<T> from the standard library to wrap data when multiple owners need to share it within a single thread. Clone the Rc instance to create new owners; the data is freed only when the last owner is dropped.
use std::rc::Rc;
fn main() {
let data = Rc::new(String::from("shared"));
let clone = Rc::clone(&data);
println!("Count: {}", Rc::strong_count(&data));
}
Rc is a smart pointer that tracks how many parts of your code are using a specific piece of data. It acts like a shared reference counter, ensuring the data stays alive as long as anyone needs it and automatically deletes it when the last user is done. Think of it as a group of people holding onto a single document; the document is only shredded when the very last person lets go.