Rust "error[E0072]: recursive type has infinite size" in CI
A type contains itself by value, so its size would be infinite. Rust needs a known, finite size for every type, so a directly recursive field must be put behind a pointer like Box.
What this error means
cargo build fails with error[E0072]: recursive type X has infinite size, suggesting you insert some indirection (e.g., a Box). The build stops.
cargo
error[E0072]: recursive type `List` has infinite size
--> src/main.rs:1:1
|
1 | enum List { Cons(i32, List), Nil }
| ^^^^^^^^^ ---- recursive without indirection
help: insert some indirection (e.g., a `Box`) to break the cycleCommon causes
Type contains itself by value
A struct or enum has a field of its own type with no indirection, so computing its size recurses forever.
Mutually recursive types by value
Two types each hold the other by value, forming a cycle with no fixed size.
How to fix it
Box the recursive field
A Box<T> is a fixed-size pointer, breaking the size recursion.
Rust
enum List {
Cons(i32, Box<List>),
Nil,
}Use another indirection where it fits
Rc, Vec, or Option<Box<...>> also give the recursive part a known size.
Rust
struct Node {
value: i32,
children: Vec<Node>, // Vec is a fixed-size handle
}How to prevent it
- Put recursive fields behind
Box/Rc/Vecfrom the start. - Model trees and linked lists with explicit indirection.
- Run
cargo checklocally to catch infinite-size types early.
Related guides
Rust "error[E0106]: missing lifetime specifier" in CIFix Rust "error[E0106]: missing lifetime specifier" in CI -- a return type or struct holds a reference with n…
Rust "error[E0277]: trait bound is not satisfied" in CIFix Rust "error[E0277]: the trait bound `T: Trait` is not satisfied" in CI - a type doesn’t implement a requi…
Rust "error: could not compile `crate`" - Read the Real ErrorUnderstand cargo "error: could not compile `X` due to N previous errors" in CI - the summary line, not the ca…