# Rust: error[E0015]: cannot call non-const function `String::from` in constants

**Code:** `E0015: cannot call non-const function in constants` · **Category:** Rust · **Seen on:** Rust

A `const`/`static` initializer used something that can't run at compile time.

## 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?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/rust/e0015-non-const-fn-in-const)
