Skip to content
← All Rust tracks

Smart Pointers and Interior Mutability

Rust

Choose between owned, shared and borrowed data using Box, Rc, Weak, RefCell and Cow. Compare runtime borrow checks with arena-based designs.

This track uses Rust. You can make it your practice mode to browse related content across the site.

22 exercises

4 articles

Explore freely. Sign in to keep your progress.

Your learning sequence

  1. 1 Not solved yet. The stack, the heap, and Box<T> Practice · Easy
  2. 2 Not solved yet. Recursive types need indirection: E0072 Practice · Easy
  3. 3 Enum layout: niches, guarantees and measurements Read
  4. 4 Not solved yet. Reduce an enum’s inline size with Box Practice · Medium
  5. 5 Not solved yet. Rc<T>: shared ownership by reference counting Practice · Medium
  6. 6 Not solved yet. Trace shared ownership with Rc Practice · Medium
  7. 7 Not solved yet. Rc::make_mut: copy-on-write shared data Practice · Hard
  8. 8 Not solved yet. Rc<str>: sharing interned strings Practice · Medium
  9. 9 Shared does not mean immutable Read
  10. 10 Not solved yet. Cell<T>: mutation by moving values in and out Practice · Medium
  11. 11 Not solved yet. RefCell<T>: borrowing checked at runtime Practice · Medium
  12. 12 Not solved yet. When RefCell panics: re-entrancy and borrows held too long Practice · Hard
  13. 13 Not solved yet. Keep a guard alive with a named binding Practice · Medium
  14. 14 Not solved yet. RAII: build your own guard Practice · Medium
  15. 15 Not solved yet. Rc<RefCell<T>>: shared inboxes and an arena alternative Practice · Hard
  16. 16 Not solved yet. Reference cycles leak memory Practice · Hard
  17. 17 Not solved yet. Weak<T>: non-owning references that break cycles Practice · Hard
  18. 18 Not solved yet. Rc::new_cyclic: self-reference in one step Practice · Hard
  19. 19 Deref, coercion and smart-pointer APIs Read
  20. 20 Not solved yet. OnceCell: write-once interior mutability Practice · Medium
  21. 21 Not solved yet. static mut is dead: what edition 2024 changed Practice · Medium
  22. 22 Not solved yet. Reject stale handles with a generational arena Practice · Hard
  23. 23 Not solved yet. Build tree paths with an arena and Rc Practice · Hard
  24. 24 Not solved yet. Drop order in composite structures Practice · Medium
  25. 25 Not solved yet. Build Cell<T> and RefCell<T> from scratch Practice · Hard
  26. 26 Choosing a smart pointer Read

Check yourself

4 questions · not saved while you're signed out

Use these questions to check your understanding and compare explanations. Your score does not affect track completion.

0 / 4 correct

What does Rc::clone do in this program?

Choose one answer to reveal the explanations. Answers are kept only while this page is open.

use std::rc::Rc;

fn main() {
    let value = Rc::new(String::from("hello"));
    {
        let other = Rc::clone(&value);
        println!("owners: {}", Rc::strong_count(&value));
        println!("same allocation: {}", Rc::ptr_eq(&value, &other));
    }
    println!("owners: {}", Rc::strong_count(&value));
}
Question 1 of 4