Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Choose a representation for linked data

Start with the ownership and operations the application needs:

NeedStarting representationImportant cost or constraint
Stack or sequential datalist[T]Contiguous storage; growth can allocate.
Queue with two endsstd.dequeRing buffer; amortized constant-time insertion and constant-time removal.
Tree with uniquely owned childrenClasses/enums with boxes or child listsSubtrees move as owners; boxes allocate per node.
Parent/back links, shared targets, or cyclesList owning nodes plus IDsIDs need validation; removal/reuse needs an identity policy.
Frequent rewiring at known nodesLinked owners or arena linksFinding the node may dominate; measure allocation and locality costs.

These are starting points, not performance rankings. The current box runtime has a 32-byte header before the payload, in addition to allocator overhead and alignment. Treat this as an implementation detail, not a stable ABI or proof that one representation wins. See memory architecture.

Boxes allow direct field access and transfer without explicit dereferences in many contexts. They do not permit two owners of the same node. Current borrowing rules also reject stored references and simultaneous mutable loans into distinct indices of one list. Sequential updates such as nodes[a].next = Some(b) work; the IDs themselves borrow nothing.

Automatic destruction of recursive owners uses bounded native stack space and does not allocate. Recursive user traversal and destructor hooks do not inherit that guarantee. A descendant reference cursor handles a chain; a non-consuming general tree traversal needs a supported worklist representation. Arena IDs are one option when a list of borrowed nodes is unavailable.

An intrusive collection embeds its links in the application object. It can support multiple memberships and, in pointer-based designs, may need stable addresses and externally managed lifetimes. Arena-owned objects with integer links cover some of these uses, but do not promise stable physical addresses. The design assessment discusses this boundary and the candidate standard-library abstraction.