Use
constby default. Usestaticwhen you need a'staticreference or shared global state (e.g.static LOGGER: OnceLock<Logger>).
const: compile-time constant. Inlined at every use site. No fixed memory address.
const MAX: u32 = 100;static: single instance with a fixed memory address for the program’s lifetime. Can be referenced (&'static).
static GREETING: &str = "hello";static mut: mutable static — requires unsafe to access.
Key differences:
const | static | |
|---|---|---|
| Memory address | No (inlined) | Yes (one location) |
'static reference | No | Yes |
| Mutable | No | Yes (unsafe) |
| Interior mutability | No | Yes (Mutex, OnceLock, etc.) |