ownership
Rust Cloning a shared owner
Rc is a single-threaded reference-counted owner. Cloning the Rc creates a new owning handle to the same allocation rather than duplicating the contained data. This is a preview of smart pointers, not a recommendation to use Rc for every value. Expected output (not executed): same: true owners: 2 Why: Both handles refer to the same allocation, and both are strong owners. Common mistake: Assuming Rc::clone duplicates the String or that Rc is suitable for cross-thread shared ownership.
Formal Syntax & Structure
rust syntaxrust
Rc::clone(&owner)Working Example
Example Snippetrust
use std::rc::Rc;fn main() { let a = Rc::new(String::from("Catalog")); let b = Rc::clone(&a); println!("same: {}", Rc::ptr_eq(&a, &b)); println!("owners: {}", Rc::strong_count(&a));}