@@ -183,59 +183,9 @@ aborts the process by executing an illegal instruction.
183
183
184
184
## Const functions
185
185
186
- Functions qualified with the ` const ` keyword are const functions, as are
186
+ Functions qualified with the ` const ` keyword are [ const functions] , as are
187
187
[ tuple struct] and [ tuple variant] constructors. _ Const functions_ can be
188
- called from within [ const context] s. When called from a const context, the
189
- function is interpreted by the compiler at compile time. The interpretation
190
- happens in the environment of the compilation target and not the host. So
191
- ` usize ` is ` 32 ` bits if you are compiling against a ` 32 ` bit system, irrelevant
192
- of whether you are building on a ` 64 ` bit or a ` 32 ` bit system.
193
-
194
- If a const function is called outside a [ const context] , it is indistinguishable
195
- from any other function. You can freely do anything with a const function that
196
- you can do with a regular function.
197
-
198
- Const functions have various restrictions to make sure that they can be
199
- evaluated at compile-time. It is, for example, not possible to write a random
200
- number generator as a const function. Calling a const function at compile-time
201
- will always yield the same result as calling it at runtime, even when called
202
- multiple times. There's one exception to this rule: if you are doing complex
203
- floating point operations in extreme situations, then you might get (very
204
- slightly) different results. It is advisable to not make array lengths and enum
205
- discriminants depend on floating point computations.
206
-
207
- Exhaustive list of permitted structures in const functions:
208
-
209
- > ** Note** : this list is more restrictive than what you can write in
210
- > regular constants
211
-
212
- * Type parameters where the parameters only have any [ trait bounds]
213
- of the following kind:
214
- * lifetimes
215
- * ` Sized ` or [ ` ?Sized ` ]
216
-
217
- This means that ` <T: 'a + ?Sized> ` , ` <T: 'b + Sized> ` , and ` <T> `
218
- are all permitted.
219
-
220
- This rule also applies to type parameters of impl blocks that
221
- contain const methods.
222
-
223
- This does not apply to tuple struct and tuple variant constructors.
224
-
225
- * Arithmetic and comparison operators on integers
226
- * All boolean operators including ` && ` and ` || `
227
- * Any kind of aggregate constructor (array, ` struct ` , ` enum ` , tuple, ...)
228
- * Calls to other * safe* const functions (whether by function call or method call)
229
- * Index expressions on arrays and slices
230
- * Field accesses on structs and tuples
231
- * Reading from constants (but not statics, not even taking a reference to a static)
232
- * ` & ` and ` * ` (only dereferencing of references, not raw pointers)
233
- * ` if ` , ` if let ` , and ` match `
234
- * ` while ` , ` while let ` , and ` loop `
235
- * Casts except for raw pointer to integer casts and cast to slice
236
- * ` unsafe ` blocks and ` const unsafe fn ` are allowed, but the body/block may only do
237
- the following unsafe operations:
238
- * calls to const unsafe functions
188
+ called from within [ const context] s.
239
189
240
190
## Async functions
241
191
@@ -397,6 +347,7 @@ fn foo_oof(#[some_inert_attribute] arg: u8) {
397
347
[ _WhereClause_ ] : generics.md#where-clauses
398
348
[ _OuterAttribute_ ] : ../attributes.md
399
349
[ const context ] : ../const_eval.md#const-context
350
+ [ const functions ] : ../const_eval.md#const-functions
400
351
[ tuple struct ] : structs.md
401
352
[ tuple variant ] : enumerations.md
402
353
[ external block ] : external-blocks.md
0 commit comments