Caution

You’re reading a draft of the Ferrocene Language Specification. Some parts of this document might be missing, incomplete or incorrect. Our aim is to have the specification ready by the end of 2022.

9. Functions

Syntax

FunctionDeclaration ::=
    FunctionQualifierList fn Name 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? self

TypedSelf ::=
    mut? self TypeAscription

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 matches an input value at the site of a call expression or a method call expression to a pattern.

9:6 A self parameter or a receiver 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 irrefutable.

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 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: &param_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:

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() {}