Rust
E0072 Recursive Type Fix
A linked list node that holds the next node directly, an expression tree whose variant holds two more expressions, two structs that each hold the other: every one asks the compiler for a value that contains itself, forever, and no finite size exists. The fix is one move in three places — box one link, so that field becomes a pointer with a known size, and the rest of the type can be laid out.
Written November 26, 2026 · Tested with Rust 1.99.1 · 8 min read
- The error: a struct that holds itself
- The error: an enum variant that holds the enum
- The error: two structs that hold each other
- Which fix for which cause
Written November 26, 2026
Tested with Rust 1.99.1
What this guide is
Every value in Rust has a size the compiler must know when it lays out the type. A struct that contains itself by value breaks that requirement in the simplest possible way: the size of the struct would have to include the size of the struct, which includes the size of the struct, with no end. E0072 is the compiler refusing to start that regress — recursive type has infinite size.
The repair never changes what the data means. It changes how one link in the cycle is stored: behind a Box, the field is a single pointer, whose size is known without knowing the size of the type it points to. The chain can then be as long as the program builds it at runtime, while the type itself stays one fixed size at compile time. The three shapes below differ only in where the cycle runs — inside one struct, inside one enum, or between two structs — and in each case the compiler's help line points at the field to box.
The error: a struct that holds itself
This program defines a linked-list node whose next field is another node, stored by value:
struct Node {
value: i32,
next: Node,
}
fn main() {
println!("node");
}
$ rustc --edition 2021 broken_node.rs -o broken_node.bin
error[E0072]: recursive type `Node` has infinite size
--> broken_node.rs:1:1
|
1 | struct Node {
| ^^^^^^^^^^^
2 | value: i32,
3 | next: Node,
| ---- recursive without indirection
|
help: insert some indirection (e.g., a `Box`, `Rc`, or `&`) to break the cycle
|
3 | next: Box<Node>,
| ++++ +
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0072`.
To lay out Node, the compiler needs the size of next, which is the size of Node — the very number it is trying to compute. The span underlines next as recursive without indirection, and the help line names the repair: insert some indirection, such as a Box, to break the cycle.
The fixed program boxes the link and wraps it in Option, so a chain can also end:
struct Node {
value: i32,
next: Option<Box<Node>>,
}
fn main() {
let node = Node {
value: 1,
next: Some(Box::new(Node { value: 2, next: None })),
};
println!("{} {}", node.value, node.next.as_ref().unwrap().value);
}
$ rustc --edition 2021 fixed_node.rs -o fixed_node.bin
$ ./fixed_node.bin
1 2
Box<Node> is a pointer, so Node is now the size of an i32 plus one pointer, whatever hangs off the end. The Option is what makes the structure usable as well as legal: None is the last node, the place the recursion stops at runtime just as the Box stops it at compile time.
The error: an enum variant that holds the enum
This program defines an expression type whose Add variant holds two more expressions by value:
enum Expr {
Num(i32),
Add(Expr, Expr),
}
fn main() {
println!("expr");
}
$ rustc --edition 2021 broken_expr.rs -o broken_expr.bin
error[E0072]: recursive type `Expr` has infinite size
--> broken_expr.rs:1:1
|
1 | enum Expr {
| ^^^^^^^^^
2 | Num(i32),
3 | Add(Expr, Expr),
| ---- recursive without indirection
|
help: insert some indirection (e.g., a `Box`, `Rc`, or `&`) to break the cycle
|
3 | Add(Box<Expr>, Expr),
| ++++ +
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0072`.
An enum is as large as its largest variant, and the Add variant's size depends on the size of Expr itself — the same regress, one type over. The help line suggests boxing one field, but a tree needs both operands behind indirection; boxing only one still leaves the other stored by value inside the variant.
The fixed program boxes both operands:
enum Expr {
Num(i32),
Add(Box<Expr>, Box<Expr>),
}
fn eval(expr: &Expr) -> i32 {
match expr {
Expr::Num(n) => *n,
Expr::Add(left, right) => eval(left) + eval(right),
}
}
fn main() {
let expr = Expr::Add(Box::new(Expr::Num(1)), Box::new(Expr::Num(2)));
println!("{}", eval(&expr));
}
$ rustc --edition 2021 fixed_expr.rs -o fixed_expr.bin
$ ./fixed_expr.bin
3
Each Box<Expr> is one pointer, so the Add variant has a fixed size and Expr has a fixed, finite size — 16 bytes for the fixed type above, exactly two pointers. The evaluator walks the tree through the boxes exactly as it would have through direct fields; the only change at the call site is wrapping each subexpression in Box::new when it is built.
The error: two structs that hold each other
This program defines two structs, each containing the other by value:
struct A {
b: B,
}
struct B {
a: A,
}
fn main() {
println!("mutual");
}
$ rustc --edition 2021 broken_mutual.rs -o broken_mutual.bin
error[E0072]: recursive types `A` and `B` have infinite size
--> broken_mutual.rs:1:1
|
1 | struct A {
| ^^^^^^^^
2 | b: B,
| - recursive without indirection
...
5 | struct B {
| ^^^^^^^^
6 | a: A,
| - recursive without indirection
|
help: insert some indirection (e.g., a `Box`, `Rc`, or `&`) to break the cycle
|
2 ~ b: Box<B>,
3 | }
4 |
5 | struct B {
6 ~ a: Box<A>,
|
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0072`.
Here the cycle runs between types: the size of A needs the size of B, and the size of B needs the size of A. The error names both types — recursive types A and B have infinite size — and underlines one field in each. Breaking the cycle only takes one boxed link, not two: once either direction goes through a pointer, both sizes can be computed.
The fixed program boxes the link from A to B, and leaves the return link as an optional box as well so a pair can point back when one is built:
struct A {
value: i32,
b: Option<Box<B>>,
}
struct B {
value: i32,
a: Option<Box<A>>,
}
fn main() {
let a = A {
value: 1,
b: Some(Box::new(B { value: 2, a: None })),
};
let b = a.b.as_ref().unwrap();
println!("{} {} {}", a.value, b.value, b.a.is_none());
}
$ rustc --edition 2021 fixed_mutual.rs -o fixed_mutual.bin
$ ./fixed_mutual.bin
1 2 true
With b: Option<Box<B>>, A is an i32 plus one pointer. B keeps its own Option<Box<A>> field, which is also one pointer, so B is an i32 plus one pointer too — the cycle is broken at the first boxed link, and the second box is there because the data may genuinely point back, not because the size computation needs it.
Which fix for which cause
| Cause | Message | Fix |
|---|---|---|
| Struct holds itself | E0072, recursive type has infinite size | Box the self field, in an Option if the chain ends |
| Enum variant holds the enum | E0072, recursive type has infinite size | Box the variant's recursive fields |
| Two types hold each other | E0072, recursive types have infinite size | Box one link in the cycle |
Checklist
- Find the field the span calls recursive without indirection. That field, not the whole type, is where the fix lands.
- Box that field.
Box<T>is one pointer, and a pointer's size never depends on the size ofT. - Wrap the box in
Optionwhen the chain needs a way to end — a list, tree, or graph with no empty case is a structure no program can finish building. - In an enum, box every recursive field of the variant, not only the one the help line rewrites.
- In a mutual cycle, one boxed link breaks the size regress for both types. Add boxes in the other direction only when the data needs them.
- If the recursion is through a collection such as
Vec<T>, no fix is needed: aVecis already a pointer to heap data, so it already supplies the indirection.
Related guides on this site
- Rust Ownership and Borrowing — the concepts guide behind the fix: what a
Boxowns, and why a pointer has a size the compiler can state without looking through it. - Fix Rust E0308 "mismatched types" — the companion repair manual for two types that met and disagreed, where this guide is the manual for one type that contains itself.
- Fix Rust E0063 "missing fields in struct initializer" — the struct error that appears once the type is legal and an initializer forgets a field, including a boxed one added by an E0072 fix.
- Fix Rust E0507 "cannot move out of borrowed content" — the error that follows when code tries to move a value out from behind a reference or a box instead of borrowing it.
Sources
- The Rust Reference, Type system — for sized types having a size known at compile time, and for dynamically sized types being usable only through a pointer.
- The Rust Standard Library, std::boxed::Box — for
Boxas a pointer type for heap allocation, and for recursive types with an unknown size at compile time being one of its documented uses. - Rust Compiler Error Index, E0072 — for a recursive type having an infinite size, and for its erroneous and corrected examples built on a recursive list type.
Keep Exploring
The full Rust hub \u2014 ownership, structs and enums, async, and measured comparisons.
Every guide on the site in one list, grouped by topic.
Every browser-only checker and verifier on the site.
The C++, Python, Sysadmin, Rust, Go, and TypeScript hubs.