Rust
E0435 Non-Constant Value in Constant Fix
A `const` is a compile-time value. If its initializer names a `let` variable - a value that exists only when the program runs - there is nothing for the compiler to evaluate, and the const does not compile.
Written November 28, 2026 · Tested with Rust 1.99.1 · 11 min read
- The rule underneath: a constant is a compile-time value
- The first failure: a let value in a const
- The second failure: a plain function call in a const
- The first fix: make the called function a const fn
Written November 28, 2026
Tested with Rust 1.99.1
What this guide is
Error E0435 fires when a constant expression uses a value that is not constant. The classic shape is small: a variable bound with let inside a function, then named as the value of a const declared right below it. The variable holds a perfectly good number, and the code looks harmless, but the two bindings live in different worlds. The let value is produced when the program runs; the const value must be settled before the program exists. The compiler refuses to bridge that gap, and the refusal is E0435.
This guide reproduces that failure, then separates a close case that carries a different error code: calling an ordinary function in a const initializer is not E0435 but E0015. It then gives the three repairs - make the called function a const fn, replace the const with a let when a run-time value is what the code actually needs, or build the const from literals and other constants - and closes with the same E0435 in its other common home, an array length.
The rule underneath: a constant is a compile-time value
The official E0435 error page defines the error in one line: a non-constant value was used in a constant expression, and it adds the gloss that matters - "constant" means "a compile-time value". The Rust Reference's constant items page fills in what a const is: a named constant value that is not associated with a specific memory location, inlined wherever it is used, and - for free constants - always evaluated at compile time. A value that only comes into being when a function runs cannot be evaluated before the program runs, so it cannot feed a const.
That rule decides what may appear on the right side of a const. A literal is fine. Another const is fine, and arithmetic on constants is fine. A call is fine only when the function is a const fn, a function the compiler itself can run while compiling. A let variable is never fine there, no matter how obviously fixed its value looks, because the binding is a run-time binding. The compiler judges the kind of value, not how predictable the number happens to be.
The first failure: a let value in a const
The minimal program binds a number with let and then tries to store that same number in a const:
fn main() {
let size = 4;
const CAP: usize = size;
println!("{size} {CAP}");
}
$ rustc --edition 2021 p1_fail_let_in_const.rs -o p1_fail_let_in_const.bin
error[E0435]: attempt to use a non-constant value in a constant
--> p1_fail_let_in_const.rs:3:24
|
3 | const CAP: usize = size;
| ^^^^ non-constant value
|
help: consider using `let` instead of `const`
|
3 - const CAP: usize = size;
3 + let CAP: usize = size;
|
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0435`.
The span underlines size in the const line and labels it "non-constant value" - the variable itself is the fault, not the number it holds. The help offers the compiler's own first suggestion: consider using let instead of const, shown as a rewrite of the line. That suggestion is one of the real fixes, covered below, but it is a change of meaning, not a spelling fix. Note what is absent: there is no complaint about size being unused. The variable is also read by the println! line, so the error stands alone. In the stripped-down form where the failed const is the variable's only use, the same program also draws an unused-variable warning for size on top of the error, because from the compiler's side that use never counted.
The second failure: a plain function call in a const
A close cousin is worth separating before the fixes, because it looks like the same mistake and is not. Calling an ordinary function to compute a constant's value:
fn next() -> usize {
4
}
fn main() {
const CAP: usize = next();
println!("{CAP}");
}
$ rustc --edition 2021 p2_fail_nonconst_fn.rs -o p2_fail_nonconst_fn.bin
error[E0015]: cannot call non-const function `next` in constants
--> p2_fail_nonconst_fn.rs:6:24
|
6 | const CAP: usize = next();
| ^^^^^^
|
note: function `next` is not const
--> p2_fail_nonconst_fn.rs:1:1
|
1 | fn next() -> usize {
| ^^^^^^^^^^^^^^^^^^
= note: calls in constants are limited to constant functions, tuple structs and tuple variants
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0015`.
This is E0015, not E0435. The header says the function cannot be called in constants, a note points back at the definition and says the function is not const, and the closing note states the general limit: calls in constants are limited to constant functions, tuple structs and tuple variants. The distinction is practical. E0435 names a value that is not constant; E0015 names a call that is not constant. They share a cause - something on the right side of a const cannot be evaluated at compile time - but the repair for the call is specific: mark the function const fn, which is the first fix below.
The first fix: make the called function a const fn
When the value a const needs comes from a function, the function has to be one the compiler can evaluate itself. Marking it const fn makes the call in the initializer legal:
const fn next() -> usize {
4
}
fn main() {
const CAP: usize = next();
println!("{CAP}");
}
$ rustc --edition 2021 p3_fix_const_fn.rs -o p3_fix_const_fn.bin
$ ./p3_fix_const_fn.bin
4
The program compiles and prints 4. Nothing else changed: the same function body, the same call, the same const. A const fn can still be called in ordinary run-time code exactly as before, so marking a simple function const does not take anything away from its other uses. This is the right fix when the computation is genuinely a compile-time computation - arithmetic, table building, size calculations - and the result is meant to live in constants.
The second fix: replace the const with a let
The compiler's own suggestion in the first failure is the right repair when the value was never truly a compile-time value. If the number comes from run-time data - user input, a file, a calculation on run-time values - then the binding should be a let too:
fn main() {
let size = 4;
let cap = size;
println!("{cap}");
}
$ rustc --edition 2021 p4_fix_let.rs -o p4_fix_let.bin
$ ./p4_fix_let.bin
4
This compiles and prints 4, but the meaning has changed, and the change is the point. cap is now an ordinary run-time variable: it is computed when main runs, it lives in the function like any other local, and it cannot be used where a constant expression is required - it cannot size an array, fill another const, or appear in any other compile-time position. Choose this fix when the value depends on anything that happens at run time, or when nothing downstream needs the value at compile time. Keep the const and fix its inputs instead when something downstream does.
The third fix: build the const from constants
When the const itself is right and only its ingredients are wrong, replace the run-time ingredient with a constant one. A const may be built from literals and from other constants, including arithmetic on them:
const BASE: usize = 4;
fn main() {
const CAP: usize = BASE * 2;
println!("{CAP}");
}
$ rustc --edition 2021 p5_fix_literal_const.rs -o p5_fix_literal_const.bin
$ ./p5_fix_literal_const.bin
8
BASE is itself a const, so BASE * 2 is a constant expression, CAP settles to 8 at compile time, and the program prints 8. This is the fix the E0435 error page itself prescribes: replace the value with a constant - its examples swap the offending variable for a literal and for a named const. In real code the pattern is usually this one: the number that was sitting in a let because it was written first becomes a named const at the top of the file, and every constant that needs it reads the const.
Neighboring error: the same code in an array length
E0435 is not confined to const initializers. Any position that requires a constant expression draws it, and the most common other position is an array length. The error page's own example is this shape - a let variable used as the length of an array type:
fn main() {
let foo = 42;
let a: [u8; foo] = [0; 42];
println!("{}", a.len());
}
$ rustc --edition 2021 p6_neighbour_array_let.rs -o p6_neighbour_array_let.bin
error[E0435]: attempt to use a non-constant value in a constant
--> p6_neighbour_array_let.rs:3:17
|
3 | let a: [u8; foo] = [0; 42];
| ^^^ non-constant value
|
help: consider using `const` instead of `let`
|
2 - let foo = 42;
2 + const foo: /* Type */ = 42;
|
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0435`.
Same code, same wording, one telling difference: the help now points the other way. There is no const in the program to demote to a let, so the compiler instead suggests promoting the variable - consider using const instead of let, with a rewrite of the let line. Promoting it is the fix:
fn main() {
const FOO: usize = 42;
let a: [u8; FOO] = [0; FOO];
println!("{}", a.len());
}
$ rustc --edition 2021 p7_neighbour_array_const.rs -o p7_neighbour_array_const.bin
$ ./p7_neighbour_array_const.bin
42
With FOO a const, the length is a constant expression, the array has a fixed size the compiler knows, and the program prints 42. Read together, the two array programs and the first failure are one rule seen from three sides: wherever the language asks for a compile-time value - a const initializer, an array length - a let variable cannot answer, and a const can. An E0435 span inside a type, pointing at a length, is this case; an E0435 span in a const initializer is the first failure. The position tells you which end to change.
Decision summary
- The header says "attempt to use a non-constant value in a constant" (E0435) and the span underlines a variable in a
constinitializer: the value comes from alet- either promote that value to aconst, or demote the binding to alet, depending on what the value is for. - The help under an E0435 says "consider using
letinstead ofconst": the compiler is offering the demotion - take it only when nothing needs the value at compile time. - The header instead says "cannot call non-const function" (E0015), with a note that the function is not const: the initializer calls an ordinary function - mark it
const fnif the computation belongs at compile time. - The E0435 span sits inside an array type, at the length, and the help says "consider using
constinstead oflet": the length comes from aletvariable - promote the variable to aconst. - The value depends on run-time data such as input or a file: it can never be a
const- keep it inletbindings all the way down and do not force it into constant positions.
Checklist
- Every
constinitializer is built only from literals, other constants, and calls toconst fnfunctions - noletvariable appears in it, however fixed its value looks. - Every function called from a
constinitializer is declaredconst fn. - A value that comes from run-time data stays in a
letand is never named in aconstinitializer, an array length, or any other constant position. - Array lengths and other constant positions name a
constor a literal, never aletvariable. - The repaired program compiles, runs, and prints the value the constant was meant to hold.
Related guides
- E0381 Uninitialized Use Fix - the binding-side sibling: a variable is read before any value is stored in it, where here the variable holds a value and the fault is the compile-time position it is read into.
- E0121 Placeholder Type Fix | Veldica - the type-position sibling: the placeholder
_is used where the compiler needs a real type, another case of a position demanding something definite that the code did not supply. - Fix Rust E0282 "type annotations needed" — a Vec::new Never Filled, a collect() Followed by len(), a Bare None, and a Generic Function Returning Default | Veldica - the inference-side sibling: the compiler cannot settle a type from the code given, where E0435 is the compiler refusing to settle a value at compile time from run-time material.
Sources
- Rust, Error codes index: E0435 - the definition of the error: a non-constant value was used in a constant expression, "constant" means "a compile-time value", and the stated fix is to replace the value with a constant, shown with a literal and with a named
constused as an array length - the rule, the third fix, and the neighboring array-length case in this guide. - The Rust Reference, Constant items - what a
constis underneath: a named constant value not associated with a specific memory location, inlined wherever it is used, and free constants are always evaluated at compile time - the reason a run-timeletvalue cannot feed one, and the contrast behind theletfix.
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.