11# Constant evaluation
2+ r[ const-eval]
23
4+ r[ const-eval.general]
35Constant evaluation is the process of computing the result of
46[ expressions] during compilation. Only a subset of all expressions
57can be evaluated at compile-time.
68
79## Constant expressions
810
11+ r[ const-eval.const-expr]
12+
13+ r[ const-eval.const-expr.general]
914Certain forms of expressions, called constant expressions, can be evaluated at
10- compile time. In [ const contexts] ( #const-context ) , these are the only allowed
11- expressions, and are always evaluated at compile time. In other places, such as
12- [ let statements] , constant expressions * may*
13- be, but are not guaranteed to be, evaluated at compile time. Behaviors such as
14- out of bounds [ array indexing] or [ overflow] are compiler errors if the value
15+ compile time.
16+
17+ r[ const-eval.const-expr.const-context]
18+ In [ const contexts] ( #const-context ) , these are the only allowed
19+ expressions, and are always evaluated at compile time.
20+
21+ r[ const-eval.const-expr.runtime-context]
22+ In other places, such as [ let statements] , constant expressions * may* be, but are not guaranteed to be, evaluated at compile time.
23+
24+ r[ const-eval.const-expr.error]
25+ Behaviors such as out of bounds [ array indexing] or [ overflow] are compiler errors if the value
1526must be evaluated at compile time (i.e. in const contexts). Otherwise, these
1627behaviors are warnings, but will likely panic at run-time.
1728
29+
30+ r[ const-eval.const-expr.list]
1831The following expressions are constant expressions, so long as any operands are
1932also constant expressions and do not cause any [ ` Drop::drop ` ] [ destructors ] calls
2033to be run.
2134
35+ r[ const-eval.const-expr.literal]
2236* [ Literals] .
37+
38+ r[ const-eval.const-expr.parameter]
2339* [ Const parameters] .
40+
41+ r[ const-eval.const-expr.path-item]
2442* [ Paths] to [ functions] and [ constants] .
2543 Recursively defining constants is not allowed.
44+
45+ r[ const-eval.const-expr.path-static]
2646* Paths to [ statics] . These are only allowed within the initializer of a static.
47+
48+ r[ const-eval.const-expr.tuple]
2749* [ Tuple expressions] .
50+
51+ r[ const-eval.const-expr.array]
2852* [ Array expressions] .
53+
54+ r[ const-eval.const-expr.constructor]
2955* [ Struct] expressions.
56+
57+ r[ const-eval.const-expr.block]
3058* [ Block expressions] , including ` unsafe ` and ` const ` blocks.
3159 * [ let statements] and thus irrefutable [ patterns] , including mutable bindings
3260 * [ assignment expressions]
3361 * [ compound assignment expressions]
3462 * [ expression statements]
63+
64+ r[ const-eval.const-expr.field]
3565* [ Field] expressions.
66+
67+ r[ const-eval.const-expr.index]
3668* Index expressions, [ array indexing] or [ slice] with a ` usize ` .
69+
70+ r[ const-eval.const-expr.range]
3771* [ Range expressions] .
72+
73+ r[ const-eval.const-expr.closure]
3874* [ Closure expressions] which don't capture variables from the environment.
75+
76+ r[ const-eval.const-expr.builtin-arith-logic]
3977* Built-in [ negation] , [ arithmetic] , [ logical] , [ comparison] or [ lazy boolean]
4078 operators used on integer and floating point types, ` bool ` , and ` char ` .
79+
80+ r[ const-eval.const-expr.shared-ref]
4181* Shared [ borrow] s, except if applied to a type with [ interior mutability] .
82+
83+ r[ const-eval.const-expr.deref]
4284* The [ dereference operator] except for raw pointers.
85+
86+ r[ const-eval.const-expr.group]
4387* [ Grouped] expressions.
88+
89+ r[ const-eval.const-expr.cast]
4490* [ Cast] expressions, except
4591 * pointer to address casts and
4692 * function pointer to address casts.
93+
94+ r[ const-eval.const-expr.const-fn]
4795* Calls of [ const functions] and const methods.
96+
97+ r[ const-eval.const-expr.loop]
4898* [ loop] , [ while] and [ ` while let ` ] expressions.
99+
100+ r[ const-eval.const-expr.if-match]
49101* [ if] , [ ` if let ` ] and [ match] expressions.
50102
51103## Const context
52104
105+ r[ const-eval.const-context]
106+
107+
108+ r[ const-eval.const-context.general]
53109A _ const context_ is one of the following:
54110
111+ r[ const-eval.const-context.array-length]
55112* [ Array type length expressions]
113+
114+ r[ const-eval.const-context.repeat-length]
56115* [ Array repeat length expressions] [ array expressions ]
116+
117+ r[ const-eval.const-context.init]
57118* The initializer of
58119 * [ constants]
59120 * [ statics]
60121 * [ enum discriminants]
122+
123+ r[ const-eval.const-context.generic]
61124* A [ const generic argument]
125+
126+ r[ const-eval.const-context.block]
62127* A [ const block]
63128
64129## Const Functions
65130
66- A _ const fn_ is a function that one is permitted to call from a const context. Declaring a function
131+ r[ const-eval.const-fn]
132+
133+ r[ const-eval.const-fn.general]
134+ A _ const fn_ is a function that one is permitted to call from a const context.
135+
136+ r[ const-eval.const-fn.usage]
137+ Declaring a function
67138` const ` has no effect on any existing uses, it only restricts the types that arguments and the
68139return type may use, as well as prevent various expressions from being used within it. You can freely
69140do anything with a const function that you can do with a regular function.
70141
142+ r[ const-eval.const-fn.const-context]
71143When called from a const context, the function is interpreted by the
72144compiler at compile time. The interpretation happens in the
73145environment of the compilation target and not the host. So ` usize ` is
74146` 32 ` bits if you are compiling against a ` 32 ` bit system, irrelevant
75147of whether you are building on a ` 64 ` bit or a ` 32 ` bit system.
76148
149+ r[ const-eval.const-fn.restriction]
77150Const functions have various restrictions to make sure that they can be
78151evaluated at compile-time. It is, for example, not possible to write a random
79152number generator as a const function. Calling a const function at compile-time
@@ -83,13 +156,14 @@ floating point operations in extreme situations, then you might get (very
83156slightly) different results. It is advisable to not make array lengths and enum
84157discriminants depend on floating point computations.
85158
86-
159+ r [ const-expr.const-fn.expr-allowed ]
87160Notable features that are allowed in const contexts but not in const functions include:
88161
89162* floating point operations
90163 * floating point values are treated just like generic parameters without trait bounds beyond
91164 ` Copy ` . So you cannot do anything with them but copy/move them around.
92165
166+ r[ const-expr.const-fn.fn-allowed]
93167Conversely, the following are possible in a const function, but not in a const context:
94168
95169* Use of generic type and lifetime parameters.
0 commit comments