@@ -114,7 +114,7 @@ opposite functionality to [external blocks]. Whereas external
114
114
blocks allow Rust code to call foreign code, extern functions with bodies
115
115
defined in Rust code _ can be called by foreign code_ . They are defined in the
116
116
same way as any other Rust function, except that they have the ` extern `
117
- modifier .
117
+ qualifier .
118
118
119
119
``` rust
120
120
// Declares an extern fn, the ABI defaults to "C"
@@ -162,33 +162,32 @@ attributes].
162
162
163
163
## Const functions
164
164
165
- Functions can be ` const ` , meaning they can be called from within
166
- [ const contexts] . When called from a const context, the function is interpreted
167
- by the compiler at compile time. The interpretation happens in the environment
168
- of the compilation target and not the host. So ` usize ` is ` 32 ` bits if you are
169
- compiling against a ` 32 ` bit system, irrelevant of whether you are building on
170
- a ` 64 ` bit or a ` 32 ` bit system.
165
+ Functions qualified with the ` const ` keyword are const functions. _ Const
166
+ funcions _ can be called from within [ const contexts] . When called from a const
167
+ context, the function is interpreted by the compiler at compile time. The
168
+ interpretation happens in the environment of the compilation target and not the
169
+ host. So ` usize ` is ` 32 ` bits if you are compiling against a ` 32 ` bit system,
170
+ irrelevant of whether you are building on a ` 64 ` bit or a ` 32 ` bit system.
171
171
172
- If a const function is called outside a " const context" , it is indistinguishable
172
+ If a const function is called outside a [ const context] , it is indistinguishable
173
173
from any other function. You can freely do anything with a const function that
174
174
you can do with a regular function.
175
175
176
- const functions have various restrictions to makes sure that you cannot define a
177
- const function that can't be evaluated at compile-time. It is, for example, not
178
- possible to write a random number generator as a const function. Calling a
179
- const function at compile-time will always yield the same result as calling it at
180
- runtime, even when called multiple times. There's one exception to this rule:
181
- if you are doing complex floating point operations in extreme situations,
182
- then you might get (very slightly) different results.
183
- It is adviseable to not make array lengths and enum discriminants depend
184
- on floating point computations.
176
+ Const functions have various restrictions to makes sure that they can't be
177
+ evaluated at compile-time. It is, for example, not possible to write a random
178
+ number generator as a const function. Calling a const function at compile-time
179
+ will always yield the same result as calling it at runtime, even when called
180
+ multiple times. There's one exception to this rule: if you are doing complex
181
+ floating point operations in extreme situations, then you might get (very
182
+ slightly) different results. It is adviseable to not make array lengths and enum
183
+ discriminants depend on floating point computations.
185
184
186
185
Exhaustive list of permitted structures in const functions:
187
186
188
187
> ** Note** : this list is more restrictive than what you can write in
189
188
> regular constants
190
189
191
- * type parameters where the parameters only have any [ trait bounds]
190
+ * Type parameters where the parameters only have any [ trait bounds]
192
191
of the following kind:
193
192
* lifetimes
194
193
* ` Sized ` or [ ` ?Sized ` ]
@@ -199,16 +198,16 @@ Exhaustive list of permitted structures in const functions:
199
198
This rule also applies to type parameters of impl blocks that
200
199
contain const methods
201
200
202
- * arithmetic and comparison operators on integers
203
- * all boolean operators except for ` && ` and ` || ` which are banned since
201
+ * Arithmetic and comparison operators on integers
202
+ * All boolean operators except for ` && ` and ` || ` which are banned since
204
203
they are short-circuiting.
205
- * any kind of aggregate constructor (array, ` struct ` , ` enum ` , tuple, ...)
206
- * calls to other * safe* const functions (whether by function call or method call)
207
- * index expressions on arrays and slices
208
- * field accesses on structs and tuples
209
- * reading from constants (but not statics, not even taking a reference to a static)
204
+ * Any kind of aggregate constructor (array, ` struct ` , ` enum ` , tuple, ...)
205
+ * Calls to other * safe* const functions (whether by function call or method call)
206
+ * Index expressions on arrays and slices
207
+ * Field accesses on structs and tuples
208
+ * Reading from constants (but not statics, not even taking a reference to a static)
210
209
* ` & ` and ` * ` (only dereferencing of references, not raw pointers)
211
- * casts except for raw pointer to integer casts
210
+ * Casts except for raw pointer to integer casts
212
211
* ` const unsafe fn ` is allowed, but the body must consist of safe operations
213
212
only and you won't be able to call the ` const unsafe fn ` from within another
214
213
const function even if you use ` unsafe `
0 commit comments