E0461: Const initializer cycle
| Code | Category | Stability |
|---|---|---|
E0461 | Struct and enum | Permanent |
Explanation
The dependency graph of constant initializers contains a cycle, so there is no first constant Rue can evaluate. Declaration order does not break the cycle: forward references are allowed, but direct self-reference and indirect cycles are compile-time errors.
Likely cause
Two or more constants refer to one another through their initializers, or a constant refers to itself. Replace one dependency with a concrete base value or restructure the initializers into an acyclic dependency chain.
Examples
Create a constant-initializer cycle
const FIRST: i32 = SECOND + 1;
const SECOND: i32 = FIRST - 1;
fn main() -> i32 { FIRST }
Give the dependency chain a base value
const FIRST: i32 = SECOND + 1;
const SECOND: i32 = 41;
fn main() -> i32 { FIRST }