2
2
3
3
> ** <sup >Syntax</sup >** \
4
4
> _ Function_ :\
5
- >   ;  ; _ FunctionFront _ ` fn ` [ IDENTIFIER]   ; [ _ Generics_ ] <sup >?</sup >\
5
+ >   ;  ; _ FunctionQualifiers _ ` fn ` [ IDENTIFIER]   ; [ _ Generics_ ] <sup >?</sup >\
6
6
>   ;  ;   ;  ; ` ( ` _ FunctionParameters_ <sup >?</sup > ` ) ` \
7
7
>   ;  ;   ;  ; _ FunctionReturnType_ <sup >?</sup > [ _ WhereClause_ ] <sup >?</sup >\
8
8
>   ;  ;   ;  ; [ _ BlockExpression_ ]
9
9
>
10
- > _ FunctionFront _ :\
10
+ > _ FunctionQualifiers _ :\
11
11
>   ;  ; ` const ` <sup >?</sup > ` unsafe ` <sup >?</sup > (` extern ` _ Abi_ <sup >?</sup >)<sup >?</sup >
12
12
>
13
13
> _ Abi_ :\
@@ -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"
@@ -137,58 +137,34 @@ As non-Rust calling conventions do not support unwinding, unwinding past the end
137
137
of an extern function will cause the process to abort. In LLVM, this is
138
138
implemented by executing an illegal instruction.
139
139
140
- ## Attributes on functions
141
-
142
- [ Outer attributes] [ attributes ] are allowed on functions. [ Inner
143
- attributes] [ attributes ] are allowed directly after the ` { ` inside its [ block] .
144
-
145
- This example shows an inner attribute on a function. The function will only be
146
- available while running tests.
147
-
148
- ```
149
- fn test_only() {
150
- #![test]
151
- }
152
- ```
153
-
154
- > Note: Except for lints, it is idiomatic to only use outer attributes on
155
- > function items.
156
-
157
- The attributes that have meaning on a function are [ ` cfg ` ] , [ ` deprecated ` ] ,
158
- [ ` doc ` ] , ` export_name ` , ` link_section ` , ` no_mangle ` , [ the lint check
159
- attributes] , [ ` must_use ` ] , [ the procedural macro attributes] , [ the testing
160
- attributes] , and [ the optimization hint
161
- attributes] .
162
-
163
140
## Const functions
164
141
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.
142
+ Functions qualified with the ` const ` keyword are const functions. _ Const
143
+ funcions _ can be called from within [ const contexts] . When called from a const
144
+ context, the function is interpreted by the compiler at compile time. The
145
+ interpretation happens in the environment of the compilation target and not the
146
+ host. So ` usize ` is ` 32 ` bits if you are compiling against a ` 32 ` bit system,
147
+ irrelevant of whether you are building on a ` 64 ` bit or a ` 32 ` bit system.
171
148
172
- If a const function is called outside a " const context" , it is indistinguishable
149
+ If a const function is called outside a [ const context] , it is indistinguishable
173
150
from any other function. You can freely do anything with a const function that
174
151
you can do with a regular function.
175
152
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.
153
+ Const functions have various restrictions to makes sure that they can't be
154
+ evaluated at compile-time. It is, for example, not possible to write a random
155
+ number generator as a const function. Calling a const function at compile-time
156
+ will always yield the same result as calling it at runtime, even when called
157
+ multiple times. There's one exception to this rule: if you are doing complex
158
+ floating point operations in extreme situations, then you might get (very
159
+ slightly) different results. It is adviseable to not make array lengths and enum
160
+ discriminants depend on floating point computations.
185
161
186
162
Exhaustive list of permitted structures in const functions:
187
163
188
164
> ** Note** : this list is more restrictive than what you can write in
189
165
> regular constants
190
166
191
- * type parameters where the parameters only have any [ trait bounds]
167
+ * Type parameters where the parameters only have any [ trait bounds]
192
168
of the following kind:
193
169
* lifetimes
194
170
* ` Sized ` or [ ` ?Sized ` ]
@@ -199,20 +175,43 @@ Exhaustive list of permitted structures in const functions:
199
175
This rule also applies to type parameters of impl blocks that
200
176
contain const methods
201
177
202
- * arithmetic and comparison operators on integers
203
- * all boolean operators except for ` && ` and ` || ` which are banned since
178
+ * Arithmetic and comparison operators on integers
179
+ * All boolean operators except for ` && ` and ` || ` which are banned since
204
180
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)
181
+ * Any kind of aggregate constructor (array, ` struct ` , ` enum ` , tuple, ...)
182
+ * Calls to other * safe* const functions (whether by function call or method call)
183
+ * Index expressions on arrays and slices
184
+ * Field accesses on structs and tuples
185
+ * Reading from constants (but not statics, not even taking a reference to a static)
210
186
* ` & ` and ` * ` (only dereferencing of references, not raw pointers)
211
- * casts except for raw pointer to integer casts
187
+ * Casts except for raw pointer to integer casts
212
188
* ` const unsafe fn ` is allowed, but the body must consist of safe operations
213
189
only and you won't be able to call the ` const unsafe fn ` from within another
214
190
const function even if you use ` unsafe `
215
191
192
+ ## Attributes on functions
193
+
194
+ [ Outer attributes] [ attributes ] are allowed on functions. [ Inner
195
+ attributes] [ attributes ] are allowed directly after the ` { ` inside its [ block] .
196
+
197
+ This example shows an inner attribute on a function. The function will only be
198
+ available while running tests.
199
+
200
+ ```
201
+ fn test_only() {
202
+ #![test]
203
+ }
204
+ ```
205
+
206
+ > Note: Except for lints, it is idiomatic to only use outer attributes on
207
+ > function items.
208
+
209
+ The attributes that have meaning on a function are [ ` cfg ` ] , [ ` deprecated ` ] ,
210
+ [ ` doc ` ] , ` export_name ` , ` link_section ` , ` no_mangle ` , [ the lint check
211
+ attributes] , [ ` must_use ` ] , [ the procedural macro attributes] , [ the testing
212
+ attributes] , and [ the optimization hint
213
+ attributes] .
214
+
216
215
[ IDENTIFIER ] : identifiers.html
217
216
[ RAW_STRING_LITERAL ] : tokens.html#raw-string-literals
218
217
[ STRING_LITERAL ] : tokens.html#string-literals
0 commit comments