Skip to content
Latchkey

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 cycle

Common 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/Vec from the start.
  • Model trees and linked lists with explicit indirection.
  • Run cargo check locally to catch infinite-size types early.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →