Rust

E0015 Non-Const Call in Const Context Fix

E0015 is a compile-time evaluation boundary, not a logic error: the code would run fine at runtime, but the place it is called from - a const fn, a const item, a static, an array length, a const generic argument - is evaluated by the compiler, and only const functions and const methods may be called there. The classic case is a one-line helper that works everywhere else and fails the moment a const item calls it, because the helper is not marked const. This guide works the shapes that produce E0015, shows the repairs running, and separates the neighbors that sit in the same few lines but carry different codes: E0658 for a conditionally-const method, E0379 for a trait method declared const, and E0493 for a destructor that would have to run at compile time.

Written November 30, 2026 · Tested with Rust 1.99.0 · 24 min read

By the end of this guide
  • The rule underneath: const contexts are evaluated at compile time
  • How to read the diagnostic
  • The first failures: a plain function called from const contexts
  • Standard-library calls: nothing of yours to mark

Written November 30, 2026

Tested with Rust 1.99.0

What this guide is

Error E0015 is a non-const call in a place the compiler has to evaluate itself. The error codes index opens with the cause - "A non-const function was called in a const context" - and states the rule behind it: "All functions used in a const context (constant or static expression) must be marked const." Its erroneous example is the second failure in this guide: a plain function returning Some(1), called from a const item. Its stated repair is the first fix below: "you can declare create_some as a constant function".

The practical payoff is that the repair depends on whose function is failing, and the diagnostic tells you which case you are in. When the callee is your own function or method, marking it const fn is usually the whole fix, provided its body stays inside what constant evaluation allows. When the callee belongs to the standard library and is not const, there is nothing to mark - the repairs are a const-compatible alternative that does exist, or moving the computation out of the const context to runtime, which for a static is what LazyLock is for (see the OnceLock, LazyLock, and lazy-static comparison). And some shapes that look like the same problem are not E0015 at all: the neighbors at the end of this guide carry E0658, E0379, and E0493, and none of them is fixed by adding const to a function.

Build every program in this guide with rustc --edition 2021. In the compiler transcripts below, the file names are the bare names shown on the command line.

The rule underneath: const contexts are evaluated at compile time

The Rust Reference's constant evaluation section defines the territory: "Expressions in a const context must be constant expressions" and "Expressions in const contexts are always evaluated at compile time." Its list of const contexts covers every failing shape in this guide: "The initializer of" constants and statics, "Array type length expressions", "Array repeat length expressions", and "A const generic argument", plus const blocks. On the call side, the constant expressions it allows include "Calls of const functions and const methods" - and, notably for the controls below, "loop and while expressions" and "if and match expressions".

A const function is defined by the same section: "A const function is a function that can be called from a const context. It is defined with the const qualifier", "When called from a const context, a const function is interpreted by the compiler at compile time", and "The body of a const function may only use constant expressions." That last sentence is why a const fn calling a plain function fails exactly as a const item calling it does - the body of the const fn is itself a const context. The Reference also notes the reverse direction is free: "When a const function is called from outside a const context, it behaves the same as if it did not have the const qualifier", so marking a function const never takes away its runtime use. One restriction in the same section matters for the trait shapes below: a const function may not be async, and its parameter and return types must be compatible with a const context.

A nearby code covers a different way of landing in a const context: using a runtime value, rather than a non-const call, inside a constant is E0435, covered there and not reproduced here.

How to read the diagnostic

The E0015 header names the callee and the kind of context: "cannot call non-const function double in constant functions", "in constants", or "in statics" in the probed set, and "cannot call non-const method" when the callee is a method. The span underlines the call itself. Two notes do the diagnostic work. When the callee is in your own code, a note points at its definition - "function double is not const" - which is the exact item the first repair marks. The closing note is the same across the probed E0015 failures: "calls in constant functions are limited to constant functions, tuple structs and tuple variants", with "constant functions" swapped for "constants" or "statics" to match the context. Read the context word in the header before choosing a repair: "in statics" opens the LazyLock option the compiler itself suggests in that one case, while "in constant functions" means the callee, the caller, or the call site has to change. The neighbors read differently: E0658 names a conditionally-const or not-yet-stable item, E0379 points at the const keyword in a trait declaration, and E0493 points at the binding whose value is dropped.

The first failures: a plain function called from const contexts

The smallest shape is a const fn calling a plain helper. The helper is ordinary, correct runtime code - it fails only because its caller is const:

Rust
fn double(x: u32) -> u32 {
    x * 2
}
const fn compute() -> u32 {
    double(21)
}
fn main() {
    println!("{}", compute());
}
Output
error[E0015]: cannot call non-const function `double` in constant functions
 --> p1.rs:5:5
  |
5 |     double(21)
  |     ^^^^^^^^^^
  |
note: function `double` is not const
 --> p1.rs:1:1
  |
1 | fn double(x: u32) -> u32 {
  | ^^^^^^^^^^^^^^^^^^^^^^^^
  = note: calls in constant functions 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`.

The error index's own example is the same call from a const item instead of a const fn - note the header's context word changes to "in constants", and nothing else about the shape changes:

Rust
fn create_some() -> Option<u8> {
    Some(1)
}
const FOO: Option<u8> = create_some();
fn main() {
    println!("{:?}", FOO);
}
Output
error[E0015]: cannot call non-const function `create_some` in constants
 --> p2.rs:4:25
  |
4 | const FOO: Option<u8> = create_some();
  |                         ^^^^^^^^^^^^^
  |
note: function `create_some` is not const
 --> p2.rs:1:1
  |
1 | fn create_some() -> Option<u8> {
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  = 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`.

A static initializer is a const context too, and this is the one shape where the compiler names the runtime escape hatch in its own notes - the last note below suggests LazyLock, which is fix f2:

Rust
fn seed() -> u32 {
    42
}
static VALUE: u32 = seed();
fn main() {
    println!("{}", VALUE);
}
Output
error[E0015]: cannot call non-const function `seed` in statics
 --> p3.rs:4:21
  |
4 | static VALUE: u32 = seed();
  |                     ^^^^^^
  |
note: function `seed` is not const
 --> p3.rs:1:1
  |
1 | fn seed() -> u32 {
  | ^^^^^^^^^^^^^^^^
  = note: calls in statics are limited to constant functions, tuple structs and tuple variants
  = note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)`

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0015`.

The remaining two context kinds from the Reference list fail identically in structure. A non-const call as a const generic argument - the header still says "in constants":

Rust
struct Arr<const N: usize>;
fn size() -> usize {
    3
}
type X = Arr<{ size() }>;
fn main() {
    let _x: X;
    println!("ok");
}
Output
error[E0015]: cannot call non-const function `size` in constants
 --> p9.rs:5:16
  |
5 | type X = Arr<{ size() }>;
  |                ^^^^^^
  |
note: function `size` is not const
 --> p9.rs:2:1
  |
2 | fn size() -> 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`.

And a non-const call as an array length:

Rust
fn len() -> usize {
    4
}
const A: [u8; len()] = [0; 4];
fn main() {
    println!("{:?}", A);
}
Output
error[E0015]: cannot call non-const function `len` in constants
 --> p10.rs:4:15
  |
4 | const A: [u8; len()] = [0; 4];
  |               ^^^^^
  |
note: function `len` is not const
 --> p10.rs:1:1
  |
1 | fn len() -> 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`.

In all five shapes the failing call is the same kind of call - the context is what varies, and the header's last words tell you which context you are in.

Standard-library calls: nothing of yours to mark

When the callee is a standard-library function that is not const, the "function is not const" note has no definition in your code to point at, and adding const anywhere in your code cannot change it. String::from in a const item is the common case - the header names it as a non-const associated function, the From conversion behind the call:

Rust
const S: String = String::from("hi");
fn main() {
    println!("{}", S);
}
Output
error[E0015]: cannot call non-const associated function `<String as From<&str>>::from` in constants
 --> p4.rs:1:19
  |
1 | const S: String = String::from("hi");
  |                   ^^^^^^^^^^^^^^^^^^
  |
  = 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`.

Runtime input is the extreme case of the same shape: std::env::var reads the process environment, which does not exist at compile time, and calling it from a const fn emits E0015 for the var call itself. This program's full output is worth reading whole, because the chained .ok() call adds two further errors of different kinds - an E0658 for the conditionally-const method, covered with the neighbors below, and an uncoded not-yet-stable error:

Rust
const fn read_env() -> Option<String> {
    std::env::var("HOME").ok()
}
fn main() {
    println!("{:?}", read_env());
}
Output
error[E0015]: cannot call non-const function `var::<&str>` in constant functions
 --> p13.rs:2:5
  |
2 |     std::env::var("HOME").ok()
  |     ^^^^^^^^^^^^^^^^^^^^^
  |
note: function `var` is not const
 --> /rustc/b940084d7eb6a299eb4bfeb8e34901bc051e7ac4/library/std/src/env.rs:228:0
  = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants

error[E0658]: cannot call conditionally-const method `Result::<String, VarError>::ok` in constant functions
 --> p13.rs:2:27
  |
2 |     std::env::var("HOME").ok()
  |                           ^^^^
  |
  = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
  = note: see issue #143874 <https://github.com/rust-lang/rust/issues/143874> for more information

error: `Result::<T, E>::ok` is not yet stable as a const fn
 --> p13.rs:2:5
  |
2 |     std::env::var("HOME").ok()
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 3 previous errors

Some errors have detailed explanations: E0015, E0658.
For more information about an error, try `rustc --explain E0015`.

For these shapes the repairs are the second and third fixes below: a const-compatible alternative where one exists in the probed set (String::new and Vec::new are const; String::from is not), or moving the computation to runtime.

Method calls: inherent methods and trait methods

A method call is a call, and it follows the same rule. A non-const inherent method called from a const fn emits E0015 with "method" in the header:

Rust
struct Counter {
    n: u32,
}
impl Counter {
    fn bump(&self) -> u32 {
        self.n + 1
    }
}
const fn next(c: &Counter) -> u32 {
    c.bump()
}
fn main() {
    const C: Counter = Counter { n: 41 };
    println!("{}", next(&C));
}
Output
error[E0015]: cannot call non-const method `Counter::bump` in constant functions
  --> p5.rs:10:7
   |
10 |     c.bump()
   |       ^^^^^^
   |
   = note: calls in constant functions 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`.

A standard-library method behaves the same: str::to_uppercase, which allocates a new String, is not const, and calling it inside a const fn is E0015 naming the method:

Rust
const fn shout(s: &str) -> String {
    s.to_uppercase()
}
fn main() {
    println!("{}", shout("hi"));
}
Output
error[E0015]: cannot call non-const method `str::<impl str>::to_uppercase` in constant functions
 --> p6.rs:2:7
  |
2 |     s.to_uppercase()
  |       ^^^^^^^^^^^^^^
  |
  = note: calls in constant functions 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`.

Trait methods are the hardest case in the probed set, because there is no const to add: a generic Clone::clone call inside a const fn is E0015 even though concrete types implement Clone:

Rust
const fn clone_it<T: Clone>(x: &T) -> T {
    x.clone()
}
fn main() {
    println!("{}", clone_it(&7u32));
}
Output
error[E0015]: cannot call non-const method `<T as Clone>::clone` in constant functions
 --> p7.rs:2:7
  |
2 |     x.clone()
  |       ^^^^^^^
  |
  = note: calls in constant functions 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`.

And a method of your own trait, called generically from a const fn, is E0015 with a note that explains why the implement-the-trait direction is closed on stable Rust - "method area is not const because trait Shape is not const", with the help line "const traits are not yet supported on stable Rust":

Rust
trait Shape {
    fn area(&self) -> u32;
}
struct Sq(u32);
impl Shape for Sq {
    fn area(&self) -> u32 {
        self.0 * self.0
    }
}
const fn measure<S: Shape>(s: &S) -> u32 {
    s.area()
}
fn main() {
    println!("{}", measure(&Sq(3)));
}
Output
error[E0015]: cannot call non-const method `<S as Shape>::area` in constant functions
  --> p12.rs:11:7
   |
11 |     s.area()
   |       ^^^^^^
   |
note: method `area` is not const because trait `Shape` is not const
  --> p12.rs:1:1
   |
 1 | trait Shape {
   | ^^^^^^^^^^^ this trait is not const
 2 |     fn area(&self) -> u32;
   |     ---------------------- this method is not const
   = help: const traits are not yet supported on stable Rust
   = note: calls in constant functions 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`.

For trait-method shapes in the probed set, the working repairs are structural: call the concrete type's inherent const method if it has one (f4 below is the inherent-method version of the Counter shape), inline the computation into the const fn, or move the call to runtime. Declaring the trait method itself const is a different error - E0379, with the neighbors below.

What is allowed: the controls

Not everything that looks computational is banned from a const context, and two controls in the probed set mark the boundary. A const fn built on a while loop and a match - both on the Reference's allowed list - compiles, and the const item calling it evaluates at compile time:

Rust
const fn total() -> u32 {
    let mut s = 0;
    let mut i = 0;
    while i < 5 {
        s += i;
        i += 1;
    }
    match s {
        10 => s * 2,
        _ => s,
    }
}
fn main() {
    const T: u32 = total();
    println!("{}", T);
}
Output
20

The output is 20: the loop sums 0 + 1 + 2 + 3 + 4 to 10, and the match arm doubles it. Control flow is not the problem - the call is.

Floating-point arithmetic inside a const fn also runs in the probed set:

Rust
const fn area(r: f64) -> f64 {
    r * r * 3.0
}
fn main() {
    const A: f64 = area(2.0);
    println!("{}", A);
}
Output
12

The output is 12, which is 2.0 * 2.0 * 3.0. Scope that to what it is: arithmetic on f64 values inside a const fn body, in this program - it says nothing about floating-point methods, which follow the method rule above.

One control cuts the other way, against an assumption the destructor neighbor below might create: a type implementing Drop can still be the type of a const item in the probed set, as long as nothing has to drop it during constant evaluation:

Rust
struct Guard {
    n: u32,
}
impl Drop for Guard {
    fn drop(&mut self) {}
}
const G: Guard = Guard { n: 1 };
fn main() {
    println!("{}", G.n);
}
Output
1

The output is 1. The Drop problem is not the type - it is a value being dropped inside the evaluation, which is E0493 below.

The traps: neighbors that are not the plain E0015 call

Trap
Four nearby shapes fail differently, or for a different reason.

A for loop inside a const fn is E0015, but not the non-const-function shape: a for loop desugars to an iterator call, and the header says so - "cannot use for loop on std::ops::Range<u32> in constant functions". The while version of the same sum is the control above, which runs:

Rust
const fn sum() -> u32 {
    let mut s = 0;
    for i in 0..5 {
        s += i;
    }
    s
}
fn main() {
    println!("{}", sum());
}
Output
error[E0015]: cannot use `for` loop on `std::ops::Range<u32>` in constant functions
 --> p17.rs:3:14
  |
3 |     for i in 0..5 {
  |              ^^^^
  |
  = note: calls in constant functions 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`.

So the same code on your screen can be E0015 for two different reasons: a call you wrote, or a loop the compiler rewrites into a call you did not. The repair differs - rewrite the for as a while, as in the control, rather than hunting for a function to mark const. The unstable-feature side of this territory is its own guide: E0658.

A conditionally-const method is E0658, not E0015. It appeared in the env::var program above: Result::ok is const only under conditions a generic const context cannot assume, and its diagnostic is "cannot call conditionally-const method Result::<String, VarError>::ok in constant functions", with a note pointing at the tracking issue, followed by the uncoded line "error: Result::<T, E>::ok is not yet stable as a const fn". A related uncoded shape: building a Vec with the vec! macro inside a const fn produced no error code at all in the probed set - two plain errors naming standard-library internals that are "not yet stable as a const fn":

Rust
const fn make() -> Vec<u8> {
    vec![1, 2, 3]
}
fn main() {
    println!("{:?}", make());
}
Output
error: `Box::<T>::new_uninit` is not yet stable as a const fn
 --> p14.rs:2:5
  |
2 |     vec![1, 2, 3]
  |     ^^^^^^^^^^^^^

error: `std::boxed::box_assume_init_into_vec_unsafe` is not yet stable as a const fn
 --> p14.rs:2:5
  |
2 |     vec![1, 2, 3]
  |     ^^^^^^^^^^^^^

error: aborting due to 2 previous errors

Neither of these is repaired by marking anything in your code const - the item is not stable as a const fn yet, full stop, in the probed set. The repair is a const-compatible alternative or runtime code.

Declaring a trait method const is E0379. The fix the trait shape seems to ask for - putting const on the method in the trait declaration - is rejected at the declaration itself:

Rust
trait Shape {
    const fn area(&self) -> u32;
}
fn main() {
    println!("ok");
}
Output
error[E0379]: functions in traits cannot be declared const
 --> p16.rs:2:5
  |
2 |     const fn area(&self) -> u32;
  |     ^^^^^-
  |     |
  |     functions in traits cannot be const
  |     help: remove the `const`

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0379`.

And dropping a value with a destructor inside a const fn is E0493, "destructor of Guard cannot be evaluated at compile-time". The same Guard type that was accepted as a const item above fails here, because the local g goes out of scope - is dropped - inside the evaluation:

Rust
struct Guard {
    n: u32,
}
impl Drop for Guard {
    fn drop(&mut self) {}
}
const fn make() -> u32 {
    let g = Guard { n: 7 };
    g.n
}
fn main() {
    println!("{}", make());
}
Output
error[E0493]: destructor of `Guard` cannot be evaluated at compile-time
  --> p15.rs:8:9
   |
 8 |     let g = Guard { n: 7 };
   |         ^ the destructor for this type cannot be evaluated in constant functions
 9 |     g.n
10 | }
   | - value is dropped here

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0493`.

This matches the Reference's condition on constant expressions: they are constant "so long as any operands are also constant expressions and do not cause any Drop::drop calls to be run". No const marking fixes a destructor - the repair is to keep values that need dropping out of the const evaluation, or move the work to runtime.

Fixes in reach order

First, when the callee is yours, mark it const fn - the error index's own repair, applied to its own example. The failing const item from p2, with create_some declared const, compiles and runs:

Rust
const fn create_some() -> Option<u8> {
    Some(1)
}
const FOO: Option<u8> = create_some();
fn main() {
    println!("{:?}", FOO);
}
Output
Some(1)

The output is Some(1). The same marking repairs the method shape: the Counter failure from p5, with bump declared as a const fn method, compiles and runs:

Rust
struct Counter {
    n: u32,
}
impl Counter {
    const fn bump(&self) -> u32 {
        self.n + 1
    }
}
const fn next(c: &Counter) -> u32 {
    c.bump()
}
fn main() {
    const C: Counter = Counter { n: 41 };
    println!("{}", next(&C));
}
Output
42

The output is 42, which is 41 + 1. Marking works when the callee's own body stays within constant expressions - if the body itself calls something non-const, the error simply moves to that inner call, in the probed set with the same E0015 shape. And per the Reference, the marking costs nothing at runtime: a const function called outside a const context "behaves the same as if it did not have the const qualifier".

Second, when the callee is a standard-library function that is not const, look for the const-compatible alternative the library does offer. In the probed set, the empty constructors are const even where the filling conversions are not - Vec::new and String::new evaluate at compile time, and the tuple-variant constructor Option::Some is a const call, exactly as the Reference's definition notes ("also includes tuple struct and tuple enum variant constructors"):

Rust
const V: Vec<u8> = Vec::new();
const S: String = String::new();
const N: Option<u8> = Option::Some(3);
fn main() {
    println!("{} {} {:?}", V.len(), S.len(), N);
}
Output
0 0 Some(3)

The output is 0 0 Some(3) - both containers empty, the option holding 3. Scope that to these constructors: it does not extend to String::from, which is the p4 failure, or to building a filled Vec with vec!, which is the uncoded neighbor above. Where the value genuinely has to be built by a non-const call, the const context itself has to go - which is the third fix.

Third, move the computation to runtime. For a plain call site, that means computing the value in main or another ordinary function instead of in a const item - the same call that failed in the const context is unremarkable runtime code. For a static, the compiler's own note in p3 names the mechanism: LazyLock defers the initializer to first use, at runtime, where non-const calls are allowed. The p3 failure, rewritten with the non-const seed called inside a LazyLock initializer, compiles and runs - and the direct runtime call to seed in the same program shows the function itself never needed to change:

Rust
use std::sync::LazyLock;
fn seed() -> u32 {
    42
}
static VALUE: LazyLock<u32> = LazyLock::new(|| seed());
fn main() {
    println!("{}", *VALUE);
    println!("{}", seed());
}
Output
42
42

The output is 42 twice - once from the lazily initialized static, once from the direct call. The trade is real: the value is no longer available at compile time, so it cannot feed an array length, a const generic argument, or another const item. Where a downstream const context needs the value, this fix does not apply, and the first fix - making the computation const - is the only direction in the probed set. The OnceLock and LazyLock comparison covers choosing between the lazy types.

Last, if the diagnostic is not E0015, switch playbooks: E0658 and the uncoded not-yet-stable errors mean the item is not stable as a const fn - use an alternative or runtime code, and see the E0658 guide; E0379 means a trait declaration tried to mark a method const, which stable Rust does not support - restructure to inherent const methods or runtime calls; E0493 means a value with a destructor is dropped during evaluation - keep that value out of the const context.

Decision summary

E0015, "cannot call non-const function in a const context", is one shape in this guide's probed set with five context wordings: a plain function called from a const fn ("in constant functions"), from a const item, from a const generic argument, or from an array length (all three "in constants"), and from a static initializer ("in statics", the one case where the compiler suggests LazyLock). The same code covers a non-const standard-library associated function (String::from), a runtime-input function (std::env::var), a non-const inherent method, a non-const standard-library method (str::to_uppercase), and trait methods called generically (Clone::clone, and a user trait's method, where the note adds that const traits are not yet supported on stable Rust). A for loop in a const fn is also E0015, by desugaring, while while and match run. The repairs act on the callee or the context: mark your own function or method const fn, use a const-compatible alternative where the library offers one (Vec::new, String::new, Option::Some in the probed set), or move the computation to runtime - for a static, through LazyLock. The neighbors are distinct in the probed set: a conditionally-const method is E0658, building a filled Vec with vec! in a const fn produced uncoded not-yet-stable errors, declaring a trait method const is E0379, and dropping a Drop value inside a const fn is E0493 - while the same Drop type as a const item, never dropped during evaluation, compiles and runs.

Checklist

  • Read the header's context word first: in the probed set, "in constant functions", "in constants", and "in statics" name the same rule in three places, and only the statics case carries the LazyLock suggestion.
  • Read the "is not const" note literally - in the probed set it points at the exact function whose definition gets the const marking in the first repair.
  • If the callee is a standard-library function, do not look for a definition of yours to mark - in the probed set, String::from and std::env::var are repaired by an alternative or by runtime code, never by a marking.
  • For trait methods called generically, expect no marking repair: in the probed set, Clone::clone and a user trait method both emitted E0015, and declaring the trait method const emitted E0379.
  • Check the loop form before hunting for a call: in the probed set, a for loop in a const fn is E0015 by desugaring and a while loop runs.
  • Check for a drop before blaming a call: in the probed set, a Drop value dropped inside a const fn is E0493, while the same type as a const item compiles.

Sources

  • Rust error codes index, E0015 - a non-const function was called in a const context; the erroneous example with a plain create_some function called from a const item; the rule that all functions used in a const context (constant or static expression) must be marked const; the fix example declaring create_some as a constant function, after which the same const item is no error.
  • The Rust Reference, Constant evaluation - expressions in a const context must be constant expressions and are always evaluated at compile time; the const contexts list (initializers of constants and statics, array type and repeat length expressions, const generic arguments, const blocks); constant expressions include calls of const functions and const methods, loop and while expressions, and if and match expressions, so long as they do not cause any Drop::drop calls to be run; a const function is a function that can be called from a const context, defined with the const qualifier, including tuple struct and tuple enum variant constructors; when called from a const context a const function is interpreted by the compiler at compile time, when called from outside one it behaves the same as if it did not have the const qualifier; the body of a const function may only use constant expressions.
  • Rust error codes index, E0658 - the neighboring unstable-feature code, which in this guide's probed set appears for the conditionally-const method Result::ok called in a constant function.

Keep Exploring