Rust’s ownership model says each value has one owner. When the owner goes out of scope, the value is dropped.
In the snippet below, if a is moved into b, you cannot print a afterward — ownership already moved.
1 2 3 4 5 6 7 8 9 10 11 12
leta = String::from("hello"); letb = a; println!("a = {}, b = {}", a, b);
// got error: // error[E0382]: borrow of moved value: `a` // 3 | let a = String::from("hello"); // | - move occurs because `a` has type `String`, which does not implement the `Copy` trait // 4 | let b = a; // | - value moved here // 5 | println!("a = {}, b = {}", a, b); // | ^ value borrowed here after move
Deep copy (clone)
clone gives you a second allocation. It works, but it is not cheap.
1 2 3 4 5
leta = String::from("hello"); // let b = a; letb = a.clone(); println!("a = {}, b = {}", a, b); // output: a = hello, b = hello
Reference counting (Rc)
Multiple owners without copying the payload each time — use reference counting via Rc.
Each clone bumps the count. When it hits zero, drop runs and memory is freed.
1 2 3 4 5 6 7 8
leta = Rc::new(String::from("hello")); let_b = Rc::clone(&a); { let_b = Rc::clone(&a); println!("reference count {}", Rc::strong_count(&a)); // 3, will be 2 after this block out of scope } println!("reference count {}", Rc::strong_count(&a)); // 2
Copy-on-write
The data inside an Rc is immutable through shared handles. To mutate in place, Rc::make_mut checks the strong count: if other live references exist, it clones the inner value and edits the clone; otherwise it mutates in place. That is copy-on-write — pay for a copy only when you need to diverge.
1 2 3 4 5 6 7 8 9 10
letmut a = Rc::new(String::from("hello")); letb = Rc::clone(&a); // allocate a new string (copy on write) (*Rc::make_mut(&mut a)).push_str( " world"); println!("{} {}", a, b); // hello world hello
letc = Rc::clone(&a); println!("{} {} {}", a, b, c); // hello world hello hello world
Edits stay isolated from older clones. If you want mutations visible through every handle, pair Rc with Cell or RefCell for interior mutability.
Reference cycles
Cycles break naive reference counting: counts never reach zero, so memory leaks.
The official gadget/owner example: tools point at an owner; the owner holds gadgets. With strong Rc links both ways, neither side can drop the other.
gadget_owner.gadgets.borrow_mut().push(gadget1.clone()); gadget_owner.gadgets.borrow_mut().push(gadget2.clone()); // drop the owner's handle; gadgets still hold the owner drop(gadget_owner);
println!("strong count of gadget1: {}", Rc::strong_count(&gadget1)); // strong count of gadget1: 2
println!("strong count of gadget1.owner: {}", Rc::strong_count(&gadget1.owner)); // strong count of gadget1.owner: 2
// drop gadget1 — without a cycle, the owner would go away too // here gadget1.owner's count never drops, so the owner leaks drop(gadget1);
println!("strong count of gadget2.owner: {}", Rc::strong_count(&gadget2.owner)); // strong count of gadget2.owner: 2 }
The cycle in plain terms:
1 2 3 4 5 6 7 8 9
gadgets and owner reference each other in a ring; neither side can release the other, counts never hit 0
To mutate shared data on multiple threads, wrap the inner type in Mutex or RwLock, e.g. Arc<Mutex<T>>.
One more detail: both Rc<T> and Arc<T> deref to T, so you can call T methods on the smart pointer. To avoid ambiguity with T’s own methods, people often call smart-pointer methods with full qualification, e.g. Rc::clone.