Function bodies contain statements and expressions. ;
can turn an expression into a statement. So is a function definition itself a statement or an expression? It doesn't have a semicolon on it.
fn something(a: i32) -> i32 {a + 2}
If we look at the function reference we can see the syntax laid out as such:
SyntaxFunction :FunctionQualifiers fn IDENTIFIER Generics?( FunctionParameters? )FunctionReturnType? WhereClause?BlockExpressionFunctionQualifiers :AsyncConstQualifiers? unsafe? (extern Abi?)?AsyncConstQualifiers :async | constAbi :STRING_LITERAL | RAW_STRING_LITERALFunctionParameters :FunctionParam (, FunctionParam)* ,?FunctionParam :OuterAttribute* Pattern : TypeFunctionReturnType :-> Type
The way this reads is that a Function
is the following, in order:
FunctionQualifiers
fn
IDENTIFIER
something
in our example aboveGenerics?
( FunctionParameters? )
(sometimes also called arguments)
a: i32
is our only parameterFunctionReturnType?
i32
using -> i32
WhereClause?
BlockExpression
The final piece is a BlockExpression
. So a function is some stuff followed by a block expression.
That doesn't fully answer our question though. Let's take a look at the definition of syntax for Statements
:
SyntaxStatement :;| Item| LetStatement| ExpressionStatement| MacroInvocationSemi
A Statement
is any one of these things. In our case, we only care about Item
(whose syntax is a bit more complicated). A Function
declaration, it turns out, is an Item
.
In the end a Function
is an Item
which is a Statement
, so functions are statements and thus don't need ;
on the end of a function declaration.