55>   ;  ; _ FunctionQualifiers_ ` fn ` [ IDENTIFIER]   ; [ _ GenericParams_ ] <sup >?</sup >\
66>   ;  ;   ;  ; ` ( ` _ FunctionParameters_ <sup >?</sup > ` ) ` \
77>   ;  ;   ;  ; _ FunctionReturnType_ <sup >?</sup > [ _ WhereClause_ ] <sup >?</sup >\
8- >   ;  ;   ;  ; [ _ BlockExpression_ ]
8+ >   ;  ;   ;  ; ( [ _ BlockExpression_ ] | ` ; ` )
99>
1010> _ 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 >
1512>
1613> _ Abi_ :\
1714>   ;  ; [ STRING_LITERAL] | [ RAW_STRING_LITERAL]
1815>
1916> _ 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_ ]
2128>
2229> _ FunctionParam_ :\
23- >   ;  ; [ _ OuterAttribute_ ] <sup >\* </sup > [ _ Pattern_ ] ` : ` [ _ Type_ ]
30+ >   ;  ; [ _ OuterAttribute_ ] <sup >\* </sup > (
31+ > _ FunctionParamPattern_ | ` ... ` | [ _ Type_ ] [ ^ fn-param-2015 ]
32+ > )
33+ >
34+ > _ FunctionParamPattern_ :\
35+ >   ;  ; [ _ Pattern_ ] ` : ` ( [ _ Type_ ] | ` ... ` )
2436>
2537> _ FunctionReturnType_ :\
2638>   ;  ; ` -> ` [ _ 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.
2744
2845A _ function_ consists of a [ block] , along with a name and a set of parameters.
2946Other 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 {
4360}
4461```
4562
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
4766pattern that is valid in a let binding is also valid as an argument:
4867
4968``` rust
5069fn first ((value , _ ): (i32 , i32 )) -> i32 { value }
5170```
5271
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+
5382The block of a function is conceptually wrapped in a block that binds the
5483argument patterns and then ` return ` s the value of the function's block. This
5584means that the tail expression of the block, if evaluated, ends up being
@@ -67,6 +96,9 @@ return {
6796};
6897```
6998
99+ Functions without a body block are terminated with a semicolon. This form
100+ may only appear in a [ trait] or [ external block] .
101+
70102## Generic functions
71103
72104A _ 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
187219[ tuple struct] and [ tuple variant] constructors. _ Const functions_ can be
188220called from within [ const context] s.
189221
222+ Const functions are not allowed to be [ async] ( #async-functions ) , and cannot
223+ use the [ ` extern ` function qualifier] ( #extern-function-qualifier ) .
224+
190225## Async functions
191226
192227Functions 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) {
342377[ STRING_LITERAL ] : ../tokens.md#string-literals
343378[ _BlockExpression_ ] : ../expressions/block-expr.md
344379[ _GenericParams_ ] : generics.md
380+ [ _Lifetime_ ] : ../trait-bounds.md
345381[ _Pattern_ ] : ../patterns.md
346382[ _Type_ ] : ../types.md#type-expressions
347383[ _WhereClause_ ] : generics.md#where-clauses
@@ -372,3 +408,8 @@ fn foo_oof(#[some_inert_attribute] arg: u8) {
372408[ `link_section` ] : ../abi.md#the-link_section-attribute
373409[ `no_mangle` ] : ../abi.md#the-no_mangle-attribute
374410[ 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