Rust: error[E0015]: cannot call non-const function `String::from` in constants
A const/static initializer used something that can't run at compile time.
error[E0015]: cannot call non-const function `String::from` in constantscannot call non-const method in staticscannot call non-const associated function `HashMap::<K, V>::new` in staticserror[E0015]: cannot call non-const fncalls in constants are limited to constant functions Meaning
Use const fn equivalents, literals (&str), LazyLock/OnceLock/lazy_static/once_cell for runtime-initialised statics, or compute at startup. String::from, Vec::new() (const OK), HashMap::new() (not const) differ.
Common causes
static X: String = String::from(...)- HashMap/Mutex in statics (older Rust)
- Function not marked
const fn - Heap allocation in const context
- Trait method calls in const
⚡ Quick fix
static GREETING: &str = "hi";static CACHE: LazyLock<Mutex<HashMap<..>>> = LazyLock::new(...)- Mark your own functions
const fnwhen possible
Detailed fix by platform
Rust
use std::sync::LazyLock; static CONFIG: LazyLock<Config> = LazyLock::new(|| Config::load());
How to diagnose
- Initializer — const-evaluable?
- Alternative — LazyLock?
- Fn — const fn?
🔧 Still not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Similar errors
- Blocking waiting for file lock on package cache / build directory Cargo: Blocking waiting for file lock on package cache / on build directory
- cargo: failed to get from registry (spurious network error) Cargo: warning: spurious network error (2 tries remaining): [35] SSL connect error / error: failed to download from `https://index.crates.io/...`
- cargo: failed to select a version for the requirement Cargo: error: failed to select a version for the requirement `serde = "^2.0"` — candidate versions found which didn't match: 1.0.197
Most viewed in Rust
- E0382 Rust: error[E0382]: borrow of moved value: `x`
- E0502 Rust: error[E0502]: cannot borrow `x` as mutable because it is also borrowed as immutable
- package requires rustc 1.xx or newer Cargo: error: package `home v0.5.9` cannot be built because it requires rustc 1.81 or newer, while the currently active rustc version is 1.78.0
Other ways to find it
Where this error sits
Origin: Application & API. Errors that are often confused with or caused by this one (click to open):
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
💬 What worked for others
No tips yet — did something else fix this for you?
Share what worked for you
Tips are checked by an editor before they appear. Links are removed.
Report a correction or suggest an improvement
Last updated 5 Nov 2026