5
5
>   ;  ; _ FunctionQualifiers_ ` fn ` [ IDENTIFIER]   ; [ _ GenericParams_ ] <sup >?</sup >\
6
6
>   ;  ;   ;  ; ` ( ` _ FunctionParameters_ <sup >?</sup > ` ) ` \
7
7
>   ;  ;   ;  ; _ FunctionReturnType_ <sup >?</sup > [ _ WhereClause_ ] <sup >?</sup >\
8
- >   ;  ;   ;  ; [ _ BlockExpression_ ]
8
+ >   ;  ;   ;  ; ( [ _ BlockExpression_ ] | ` ; ` )
9
9
>
10
10
> _ FunctionQualifiers_ :\
11
- >   ;  ; _ AsyncConstQualifiers_ <sup >?</sup > ` unsafe ` <sup >?</sup > (` extern ` _ Abi_ <sup >?</sup >)<sup >?</sup >
12
- >
13
- > _ AsyncConstQualifiers_ :\
14
- >   ;  ; ` async ` | ` const `
11
+ >   ;  ; ` const ` <sup >?</sup > ` async ` [ ^ async-edition ] <sup >?</sup > ` unsafe ` <sup >?</sup > (` extern ` _ Abi_ <sup >?</sup >)<sup >?</sup >
15
12
>
16
13
> _ Abi_ :\
17
14
>   ;  ; [ STRING_LITERAL] | [ RAW_STRING_LITERAL]
18
15
>
19
16
> _ FunctionParameters_ :\
20
- >   ;  ; _ FunctionParam_ (` , ` _ FunctionParam_ )<sup >\* </sup > ` , ` <sup >?</sup >
17
+ >   ;  ;   ;  ; _ SelfParam_ ` , ` <sup >?</sup >\
18
+ >   ;  ; | (_ SelfParam_ ` , ` )<sup >?</sup > _ FunctionParam_ (` , ` _ FunctionParam_ )<sup >\* </sup > ` , ` <sup >?</sup >
19
+ >
20
+ > _ SelfParam_ :\
21
+ >   ;  ; [ _ OuterAttribute_ ] <sup >\* </sup > ( _ ShorthandSelf_ | _ TypedSelf_ )
22
+ >
23
+ > _ ShorthandSelf_ :\
24
+ >   ;  ; (` & ` | ` & ` [ _ Lifetime_ ] )<sup >?</sup > ` mut ` <sup >?</sup > ` self `
25
+ >
26
+ > _ TypedSelf_ :\
27
+ >   ;  ; ` mut ` <sup >?</sup > ` self ` ` : ` [ _ Type_ ]
21
28
>
22
29
> _ FunctionParam_ :\
23
- >   ;  ; [ _ OuterAttribute_ ] <sup >\* </sup > [ _ Pattern_ ] ` : ` [ _ Type_ ]
30
+ >   ;  ; [ _ OuterAttribute_ ] <sup >\* </sup > (
31
+ > _ FunctionParamPattern_ | ` ... ` | [ _ Type_ ] [ ^ fn-param-2015 ]
32
+ > )
33
+ >
34
+ > _ FunctionParamPattern_ :\
35
+ >   ;  ; [ _ Pattern_ ] ` : ` ( [ _ Type_ ] | ` ... ` )
24
36
>
25
37
> _ FunctionReturnType_ :\
26
38
>   ;  ; ` -> ` [ _ Type_ ]
39
+ >
40
+ > [ ^ async-edition ] : The ` async ` qualifier is not allowed in the 2015 edition.
41
+ >
42
+ > [ ^ fn-param-2015 ] : Function parameters with only a type are only allowed
43
+ > in an associated function of a [ trait item] in the 2015 edition.
27
44
28
45
A _ function_ consists of a [ block] , along with a name and a set of parameters.
29
46
Other than a name, all these are optional. Functions are declared with the
@@ -43,13 +60,25 @@ fn answer_to_life_the_universe_and_everything() -> i32 {
43
60
}
44
61
```
45
62
46
- As with ` let ` bindings, function arguments are irrefutable [ patterns] , so any
63
+ ## Function parameters
64
+
65
+ As with ` let ` bindings, function parameters are irrefutable [ patterns] , so any
47
66
pattern that is valid in a let binding is also valid as an argument:
48
67
49
68
``` rust
50
69
fn first ((value , _ ): (i32 , i32 )) -> i32 { value }
51
70
```
52
71
72
+ If the first parameter is a _ SelfParam_ , this indicates that the function is a
73
+ [ method] . Functions with a self parameter may only appear as an [ associated
74
+ function] in a [ trait] or [ implementation] .
75
+
76
+ A parameter with the ` ... ` token indicates a [ variadic function] , and may only
77
+ be used as the last parameter of a [ external block] function. The variadic
78
+ parameter may have an optional identifier, such as ` args: ... ` .
79
+
80
+ ## Function body
81
+
53
82
The block of a function is conceptually wrapped in a block that binds the
54
83
argument patterns and then ` return ` s the value of the function's block. This
55
84
means that the tail expression of the block, if evaluated, ends up being
@@ -67,6 +96,9 @@ return {
67
96
};
68
97
```
69
98
99
+ Functions without a body block are terminated with a semicolon. This form
100
+ may only appear in a [ trait] or [ external block] .
101
+
70
102
## Generic functions
71
103
72
104
A _ generic function_ allows one or more _ parameterized types_ to appear in its
@@ -187,6 +219,9 @@ Functions qualified with the `const` keyword are [const functions], as are
187
219
[ tuple struct] and [ tuple variant] constructors. _ Const functions_ can be
188
220
called from within [ const context] s.
189
221
222
+ Const functions are not allowed to be [ async] ( #async-functions ) , and cannot
223
+ use the [ ` extern ` function qualifier] ( #extern-function-qualifier ) .
224
+
190
225
## Async functions
191
226
192
227
Functions may be qualified as async, and this can also be combined with the
@@ -342,6 +377,7 @@ fn foo_oof(#[some_inert_attribute] arg: u8) {
342
377
[ STRING_LITERAL ] : ../tokens.md#string-literals
343
378
[ _BlockExpression_ ] : ../expressions/block-expr.md
344
379
[ _GenericParams_ ] : generics.md
380
+ [ _Lifetime_ ] : ../trait-bounds.md
345
381
[ _Pattern_ ] : ../patterns.md
346
382
[ _Type_ ] : ../types.md#type-expressions
347
383
[ _WhereClause_ ] : generics.md#where-clauses
@@ -372,3 +408,8 @@ fn foo_oof(#[some_inert_attribute] arg: u8) {
372
408
[ `link_section` ] : ../abi.md#the-link_section-attribute
373
409
[ `no_mangle` ] : ../abi.md#the-no_mangle-attribute
374
410
[ built-in attributes ] : ../attributes.html#built-in-attributes-index
411
+ [ trait item ] : traits.md
412
+ [ method ] : associated-items.md#methods
413
+ [ associated function ] : associated-items.md#associated-functions-and-methods
414
+ [ implementation ] : implementations.md
415
+ [ variadic function ] : external-blocks.md#variadic-functions
0 commit comments