Why this fires¶
A const declaration's value is not a compile-time constant expression. Unlike let, const must be fully evaluable at compile time — it cannot read a variable, call a non-constant function, or otherwise depend on something only known at runtime.
fn now_seconds() -> int { return 0 }
const START = now_seconds()
// ^^^^^^^^^^^^^^ error[TE003]: const value must be a compile-time constant;
// use `let` for runtime valuesFix it¶
1. Use let instead¶
If the value genuinely needs to be computed at runtime, const is the wrong binding form:
let start = now_seconds() // ok — let allows runtime values2. Make the value a real constant¶
If the value should be fixed at compile time, replace the runtime expression with a literal or a comptime-evaluable expression (arithmetic on literals, references to other consts, etc.):
const START = 0 // ok — a literal
const DOUBLE_START = START * 2 // ok — comptime-foldable from another constSee also¶
TE002—const's declared type must match its value's type.- /docs/variables-and-types —
let/var/constsemantics.