Rust Reference ยท Keywords and Statements
The const keyword
Reference for Rust const: compile-time constants, associated consts, const fn, const generics parameters, inline const blocks and how const differs from static.
const declares a value that is computed at compile time and inlined wherever it is used. A constant must have an explicit type, its initializer must be a constant expression, and it has no fixed address: every use behaves like a fresh copy of the value.
The same keyword appears in three other places: const fn (a function that may be called in constant contexts), const generic parameters ([T; N]), and inline const { ... } blocks that force compile-time evaluation of an expression. Constants can also be associated with types and traits.
const MAX_USERS: usize = 1_000; const GREETING: &str = "hello"; const fn square(n: u32) -> u32 { n * n }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Syntax
const NAME: Type = constant_expression; const _: () = assert!(condition); // compile-time check const fn name(params) -> Type { ... } fn f<const N: usize>() { ... } const { expression }
| Part | Description |
|---|---|
NAME |
Written in SCREAMING_SNAKE_CASE by convention. _ is allowed for unnamed constants used only for their side check. |
Type |
Mandatory; no inference. Lifetimes in &str or &[T] default to 'static. |
| Expression | Literals, other constants, arithmetic, const fn calls, struct and array construction, if, match, while, and loop. |
Errors: a non-constant initializer (E0015), an overflow in the initializer (a compile error, not a panic at run time), and a failed assert! in a const context all stop compilation.
Usage notes
- No fixed address. Each use is inlined, so
&MAXmay produce different addresses. Usestaticwhen you need one place in memory or interior mutability that is shared. - Types with destructors are copied per use. Interior-mutable constants (
const C: Cell<i32>) are a classic trap because each use gets a new cell; Clippy warns about it. const fnlimits grow with each release. Loops,match,if, and many standard methods work now; allocation, trait-method calls on generics and floating-point operations on stable have narrower rules. When unsure, try it in aconst.- Compile-time panics.
const { assert!(N > 0) }orconst _: () = assert!(...)turns invariants into build errors. - Associated consts can be declared in traits (with or without defaults) and overridden per implementing type.
- Const generics accept integers,
boolandcharon stable; other types are not yet allowed. - Constants in patterns: a name in a
matcharm that matches a constant in scope is compared against that constant instead of creating a new binding, which surprises many people. This is also why constants use uppercase names. - Use
constoverletfor fixed numbers and tables; it also enables them in array lengths and patterns.
Related
static,let,fn,[T; N]arrays- Lessons: Variables and Mutability, Const generics