E0015: cannot call non-const function in constants Rust Origin: Application & API OSI layer 7 · Application Last updated 5 Nov 2026

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.

Exact message you may see 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
Seen on: Rust

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

  1. static GREETING: &str = "hi";
  2. static CACHE: LazyLock<Mutex<HashMap<..>>> = LazyLock::new(...)
  3. Mark your own functions const fn when possible

Detailed fix by platform

Rust

  1. use std::sync::LazyLock; static CONFIG: LazyLock<Config> = LazyLock::new(|| Config::load());

How to diagnose

  1. Initializer — const-evaluable?
  2. Alternative — LazyLock?
  3. 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:

Where this error sits

Origin: Application & API. Errors that are often confused with or caused by this one (click to open):

Blocking waiting for file lock on package cache / build directory — Cargo: Blocking waiting for file lock on package cache / on build directory (Rust)Blocking waiting for …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/...` (Rust)cargo: failed to get …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 (Rust)cargo: failed to sele…discards qualifiers — C++: error: passing 'const X' as 'this' argument discards qualifiers [-fpermissive] (C/C++)discards qualifiersE0133: call to unsafe function requires unsafe block — Rust: error[E0133]: call to unsafe function `foo` is unsafe and requires unsafe function or block (Rust)E0133: call to unsafe…E0308 — Rust: error[E0308]: mismatched types — expected `X`, found `Y` (Rust)E0308E0658: use of unstable library feature — Rust: error[E0658]: use of unstable library feature `let_chains` / `#![feature]` may not be used on the stable release channel (Rust)E0658: use of unstabl…feature edition2024 is required / Cargo.lock version too new — Cargo: error: failed to parse manifest — feature `edition2024` is required / lock file version 4 requires `-Znext-lockfile-bump` (Rust)feature edition2024 i…E0015: ca…

Explore the map of this error Full error map →

🧠 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.