9. FunctionsΒΆ
Syntax
FunctionDeclaration
::=FunctionQualifierList
fnName
GenericParameterList
? (FunctionParameterList
? )ReturnType
?WhereClause
? (FunctionBody
| ;)FunctionQualifierList
::= const? async? unsafe?AbiSpecification
?FunctionParameterList
::= (FunctionParameter
(,FunctionParameter
)* ,?) | (SelfParameter
(,FunctionParameter
)* ,?)FunctionParameter
::=OuterAttributeOrDoc
* (FunctionParameterPattern
|FunctionParameterVariadicPart
|TypeSpecification
)FunctionParameterPattern
::=PatternWithoutAlternation
(TypeAscription
| (:FunctionParameterVariadicPart
))FunctionParameterVariadicPart
::= ...ReturnType
::= ->TypeSpecification
FunctionBody
::=BlockExpression
SelfParameter
::=OuterAttributeOrDoc
* (ShorthandSelf
|TypedSelf
)ShorthandSelf
::= (&LifetimeIndication
?)? mut? selfTypedSelf
::= mut? selfTypeAscription
Legality Rules
9:1 A function is a value of a function type that models a behavior.
9:2 A function declares a unique function item type for itself.
9:3 A function qualifier is a construct that determines the role of a function.
9:4
A function shall not be subject to both keyword async
and
keyword const
.
9:5 A function parameter is a construct that yields a set of bindings that bind matched input values to names at the site of a call expression or a method call expression.
9:6
A self parameter is a function parameter expressed by keyword
self
.
9:7 A function shall not specify a self parameter unless it is an associated function.
9:8
A function shall not specify a FunctionParameterVariadicPart
unless
it is an external function.
9:9 The pattern of a function parameter shall be an irrefutable pattern.
9:10 A return type is the type of the result a function returns.
9:11 A function body is the block expression of a function.
9:12 A function shall have a function body unless it is an associated trait function or an external function.
9:13 A function body denotes a control flow boundary.
9:14 A function body of an async function denotes an async control flow boundary.
9:15 A function signature is a unique identification of a function that encompasses of its function qualifiers, name, generic parameters, function parameters, return type, and where clause.
9:16
A constant function is a function subject to keyword const
.
9:17 The function body of a constant function shall be a constant expression.
9:18 A constant function shall be callable from a constant context.
9:19
An async function is a function subject to keyword async
. An
async function of the form
async fn async_fn(param: ¶m_type) -> return_type {
/* tail expression */
}
9:20 is equivalent to function
fn async_fn<'a>(param: &'a param_type) -> impl Future<Output = return_type> + 'a {
async move {
/* tail expression */
}
}
9:21
An unsafe function is a function subject to keyword unsafe
.
9:22 The invocation of an unsafe function shall require unsafe context.
9:23 A main function is a function that acts as an entry point into a program. A main function is subject to the following restrictions:
9:24 It lacks function qualifiers
async
andunsafe
,9:25 Its ABI is Rust,
9:26 Its name is the word
main
,9:27 It lacks generic parameters,
9:28 It lacks function parameters,
9:29 It lacks a return type,
9:30 It lacks a where clause,
9:31 It has a function body.
Examples
fn eucledian_distance(left: &Point, right: &Point) -> f64 {
let x_delta_squared: f64 = (right.x - left.x).powi(2);
let y_delta_squared: f64 = (right.y - left.y).powi(2);
(x_delta_squared + y_delta_squared).sqrt()
}
fn main() {}