@@ -61,40 +61,24 @@ A _const context_ is one of the following:
61
61
* A [ const generic argument]
62
62
* A [ const block]
63
63
64
+ Const contexts that are used as parts of types (array type and repeat length
65
+ expressions as well as const generic arguments) can only make restricted use of
66
+ surrounding generic parameters: such an expression must either be a single bare
67
+ const generic parameter, or an arbitrary expression not making use of any
68
+ generics.
69
+
64
70
## Const Functions
65
71
66
72
A _ const fn_ is a function that one is permitted to call from a const context. Declaring a function
67
73
` const ` has no effect on any existing uses, it only restricts the types that arguments and the
68
- return type may use, as well as prevent various expressions from being used within it. You can freely
69
- do anything with a const function that you can do with a regular function.
74
+ return type may use, and restricts the function body to constant expressions.
70
75
71
76
When called from a const context, the function is interpreted by the
72
77
compiler at compile time. The interpretation happens in the
73
78
environment of the compilation target and not the host. So ` usize ` is
74
79
` 32 ` bits if you are compiling against a ` 32 ` bit system, irrelevant
75
80
of whether you are building on a ` 64 ` bit or a ` 32 ` bit system.
76
81
77
- Const functions have various restrictions to make sure that they can be
78
- evaluated at compile-time. It is, for example, not possible to write a random
79
- number generator as a const function. Calling a const function at compile-time
80
- will always yield the same result as calling it at runtime, even when called
81
- multiple times. There's one exception to this rule: if you are doing complex
82
- floating point operations in extreme situations, then you might get (very
83
- slightly) different results. It is advisable to not make array lengths and enum
84
- discriminants depend on floating point computations.
85
-
86
-
87
- Notable features that are allowed in const contexts but not in const functions include:
88
-
89
- * floating point operations
90
- * floating point values are treated just like generic parameters without trait bounds beyond
91
- ` Copy ` . So you cannot do anything with them but copy/move them around.
92
-
93
- Conversely, the following are possible in a const function, but not in a const context:
94
-
95
- * Use of generic type and lifetime parameters.
96
- * Const contexts do allow limited use of [ const generic parameters] .
97
-
98
82
[ arithmetic ] : expressions/operator-expr.md#arithmetic-and-logical-binary-operators
99
83
[ array expressions ] : expressions/array-expr.md
100
84
[ array indexing ] : expressions/array-expr.md#array-and-slice-indexing-expressions
0 commit comments