E0072type-check

Recursive type has infinite size

A struct or enum recursively contains itself with no indirection.

Repro

struct Node { next: Option<Node> }
// E0072: Node has infinite size (it contains itself)

Why

Node would need to be big enough to hold another Node, which holds another, ad infinitum. Rust needs a fixed size at compile time.

Fix

- Box the recursive position: struct Node { next: Option<Box<Node>> } — Box is a fixed-size pointer. - For shared ownership, use Rc/Arc: Option<Rc<Node>>. - For graphs, switch to indices into an arena: Option<NodeId>. - Single-link variants with no recursion can sometimes be flattened into a Vec.

Patterns that solve this