How to Use Rc<T> for Shared Ownership in Rust

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));
}