Skip to content

What a smart pointer is

Level: 201 · working knowledge

Stub — an outline, not a lesson. There is no runnable example behind this page yet, so nothing on it has been through the check that backs every other claim in this library. The bullets below are the questions the finished page has to answer.

One line: A smart pointer is a type that owns or manages what it points at and still behaves like a reference — Deref ↗ makes *p and method calls reach the target, and Drop runs its cleanup — so Box, Rc, Arc, String and a MutexGuard are all the same idea with different rules about who owns what.

Two finished lessons already answer much of this outline: What makes a pointer smart (a hand-written smart pointer with Deref and Drop, the Deref targets of the family, String and Vec included) and What a smart pointer costs. The Smart pointers section maps the rest.

What it has to cover

  • The two traits that make a type a smart pointer, with one hand-written MyBox<T> implementing both and printing when each runs
  • The family in one table — what each owns, whether it can be shared, whether it can cross threads, and what its Drop does:
Type Owns Shared Across threads Lesson
Box<T> one heap value no if T can Box
Rc<T> a counted heap value yes, read-only no Rc
Arc<T> the same, counted atomically yes, read-only yes Arc
Cow<'a, B> borrowed or owned — — Cow
Ref / MutexGuard a borrow of a cell or lock no guard-specific Interior mutability
  • String and Vec<T> counted as smart pointers too, and why The Book does so
  • Deref coercion: why a &Box<String> can be passed where &str is wanted, and where the chain of derefs stops
  • Choosing: Box for one owner, Rc for several on one thread, Arc for several threads, and RefCell or Mutex added only when a shared owner must also write

The trap it exists for

Implementing Deref on a newtype to inherit the inner type's methods. It compiles and reads well, then every method name of the target becomes yours to collide with, and a reader cannot tell which type a call reached.

See also

Po polsku

Inteligentny wskaźnik (smart pointer) to typ, który jest właścicielem wskazywanej wartości albo nią zarządza, a mimo to zachowuje się jak referencja: trait Deref sprawia, że *p i wywołania metod trafiają do wartości docelowej, a Drop wykonuje sprzątanie. Box, Rc, Arc, String i strażnik blokady MutexGuard to jedna idea z różnymi zasadami własności — jeden właściciel, wielu właścicieli w jednym wątku, wielu właścicieli w wielu wątkach.

Szukaj po polsku: inteligentne wskaźniki w Ruscie · rust smart pointers deref drop · rust box rc arc difference