Skip to main content
A constant item in Rust is a named value bound to a constant expression that is fully evaluated at compile time. Declared using the const keyword, constant items are not associated with a specific memory address at runtime; instead, the compiler inlines their evaluated values directly into the usage sites during code generation.

Technical Characteristics

  • Explicit Typing: Unlike let bindings, constant items strictly require an explicit type annotation. The Rust compiler does not perform type inference for constant declarations.
  • Constant Expressions: The initialization value must be a valid constant expression. This is restricted to operations that can be evaluated at compile time, such as literals, arithmetic operations on other constants, and invocations of const fn (constant functions).
  • Memory Semantics: Because constants are inlined, they do not possess a fixed, singular memory address. Taking a reference to a constant (e.g., &MY_CONST) may yield a different memory address at different evaluation sites, depending on compiler optimizations.
  • Scoping: Constant items can be declared at any scope level. This includes module-level (global scope), block-level (within functions), or within impl and trait blocks as associated constants.
  • Immutability: Constants are inherently and permanently immutable. They cannot be mutated, nor can they be declared with the mut keyword.
  • Naming Convention: By convention, constant identifiers must use SCREAMING_SNAKE_CASE. The compiler will emit a warning if this convention is violated.

Syntax Visualization

Tired of Poor Rust Skills? Fix That With Deep Grasping!Learn More