Constant evaluation
Constant evaluation is the process of computing the result of expressions during compilation. Only a subset of all expressions can be evaluated at compile-time.
Constant expressions
Certain forms of expressions, called constant expressions, can be evaluated at compile time. In const contexts, these are the only allowed expressions, and are always evaluated at compile time. In other places, such as let statements, constant expressions may be, but are not guaranteed to be, evaluated at compile time. Behaviors such as out of bounds array indexing or overflow are compiler errors if the value must be evaluated at compile time (i.e. in const contexts). Otherwise, these behaviors are warnings, but will likely panic at run-time.
The following expressions are constant expressions, so long as any operands are also constant expressions and do not cause any Drop::drop
calls to be run.
- Literals.
- Const parameters.
- Paths to functions and constants. Recursively defining constants is not allowed.
- Paths to statics. These are only allowed within the initializer of a static.
- Tuple expressions.
- Array expressions.
- Struct expressions.
-
Block expressions, including
unsafe
blocks.- let statements and thus irrefutable patterns, including mutable bindings
- assignment expressions
- compound assignment expressions
- expression statements
- Field expressions.
- Index expressions, array indexing or slice with a
usize
. - Range expressions.
- Closure expressions which don't capture variables from the environment.
- Built-in negation, arithmetic, logical, comparison or lazy boolean operators used on integer and floating point types,
bool
, andchar
. - Shared borrows, except if applied to a type with interior mutability.
- The dereference operator except for raw pointers.
- Grouped expressions.
-
Cast expressions, except
- pointer to address casts,
- function pointer to address casts, and
- unsizing casts to trait objects.
- Calls of const functions and const methods.
-
loop, while and
while let
expressions. -
if,
if let
and match expressions.
Const context
A const context is one of the following:
- Array type length expressions
- Array repeat length expressions
- The initializer of
- A const generic argument
Const Functions
A const fn is a function that one is permitted to call from a const context. Declaring a function const
has no effect on any existing uses, it only restricts the types that arguments and the return type may use, as well as prevent various expressions from being used within it. You can freely do anything with a const function that you can do with a regular function.
When called from a const context, the function is interpreted by the compiler at compile time. The interpretation happens in the environment of the compilation target and not the host. So usize
is 32
bits if you are compiling against a 32
bit system, irrelevant of whether you are building on a 64
bit or a 32
bit system.
Const functions have various restrictions to make sure that they can be evaluated at compile-time. It is, for example, not possible to write a random number generator as a const function. Calling a const function at compile-time will always yield the same result as calling it at runtime, even when called multiple times. There's one exception to this rule: if you are doing complex floating point operations in extreme situations, then you might get (very slightly) different results. It is advisable to not make array lengths and enum discriminants depend on floating point computations.
Notable features that const contexts have, but const fn haven't are:
- floating point operations
- floating point values are treated just like generic parameters without trait bounds beyond
Copy
. So you cannot do anything with them but copy/move them around.
- floating point values are treated just like generic parameters without trait bounds beyond
-
dyn Trait
types - generic bounds on generic parameters beyond
Sized
- comparing raw pointers
- union field access
-
transmute
invocations.
Conversely, the following are possible in a const function, but not in a const context:
- Use of generic type and lifetime parameters.
- Const contexts do allow limited use of const generic parameters.
© 2010 The Rust Project Developers
Licensed under the Apache License, Version 2.0 or the MIT license, at your option.
https://doc.rust-lang.org/reference/const_eval.html