Const evaluation

Const evaluation is the evaluation of expressions at compile time. It is used when an expression is assigned to a or  item. Function calls can be const-evaluated, if they are s.

Note that in optimized builds, all expressions that are guaranteed to be side-effect free and can be evaluated at compile time, will be evaluated at compile time. However, this is an optimization by LLVM, Rust's compiler backend, and does not affect language semantics. With const evaluation, we only refer to the language concept, not to LLVM optimizations. Const evaluation is independent of the optimization level and other build configurations.

Rust internally uses Miri for const evaluation. Miri is a Rust MIR interpreter.

Example
Note that  is only const-evaluated when used in a  context. A context is an expression used in a  or  item or in a function that is being const-evaluated.

Limitations
Currently, many operations (conditions, loops, panicking, etc.) in const contexts are only supported as nightly-only features. The plan is to stabilize some of these features soon.

Here's an example of what is already possible on nightly: