r[const-eval]
r[const-eval.general] 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.
r[const-eval.const-expr]
r[const-eval.const-expr.general] Certain forms of expressions, called constant expressions, can be evaluated at compile time.
r[const-eval.const-expr.const-context] In const contexts, these are the only allowed expressions, and are always evaluated at compile time.
r[const-eval.const-expr.runtime-context] In other places, such as let statements, constant expressions may be, but are not guaranteed to be, evaluated at compile time.
r[const-eval.const-expr.error] 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.
r[const-eval.const-expr.list] 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.
r[const-eval.const-expr.literal]
r[const-eval.const-expr.parameter]
r[const-eval.const-expr.path-item]
r[const-eval.const-expr.path-static]
static
items are not allowed in any constant evaluation context.extern
statics are not allowed in any constant evaluation context.static
item, then reads from any mutable static
are not allowed. A mutable static
is a static mut
item, or a static
item with an interior-mutable type.These requirements are checked only when the constant is evaluated. In other words, having such accesses syntactically occur in const contexts is allowed as long as they never get executed.
r[const-eval.const-expr.tuple]
r[const-eval.const-expr.array]
r[const-eval.const-expr.constructor]
r[const-eval.const-expr.block]
unsafe
and const
blocks.r[const-eval.const-expr.field]
r[const-eval.const-expr.index]
usize
.r[const-eval.const-expr.range]
r[const-eval.const-expr.closure]
r[const-eval.const-expr.builtin-arith-logic]
bool
, and char
.r[const-eval.const-expr.borrows]
r[const-eval.const-expr.deref]
r[const-eval.const-expr.group]
r[const-eval.const-expr.cast]
r[const-eval.const-expr.const-fn]
r[const-eval.const-expr.loop]
r[const-eval.const-expr.if-match]
r[const-eval.const-context]
r[const-eval.const-context.general] A const context is one of the following:
r[const-eval.const-context.array-length]
r[const-eval.const-context.repeat-length]
r[const-eval.const-context.init]
r[const-eval.const-context.generic]
r[const-eval.const-context.block]
Const contexts that are used as parts of types (array type and repeat length expressions as well as const generic arguments) can only make restricted use of surrounding generic parameters: such an expression must either be a single bare const generic parameter, or an arbitrary expression not making use of any generics.
r[const-eval.const-fn]
r[const-eval.const-fn.general] A const fn is a function that one is permitted to call from a const context.
r[const-eval.const-fn.usage] Declaring a function const
has no effect on any existing uses, it only restricts the types that arguments and the return type may use, and restricts the function body to constant expressions.
r[const-eval.const-fn.const-context] 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.