Functions

Syntax
Function →
    fn IDENTIFIER GenericParams?
        ( FunctionParameters? )
        FunctionReturnType? WhereClause?
        BlockExpression

FunctionParameters →
      SelfParam ,?
    | ( SelfParam , )? FunctionParam ( , FunctionParam )* ,?

SelfParam → ShorthandSelf

ShorthandSelf → ( & Lifetime? )? mut? self

FunctionParam → IdentifierBinding : Type

FunctionReturnType → -> Type

A function definition consists of a name, optional lifetime parameters, a parameter list, an optional return type, optional lifetime bounds, and a required body. Top-level functions accept only ordinary parameters, whereas functions within inherent impl blocks may also include a receiver parameter, as described below.

GenericParams declares lifetime parameters after the function name. WhereClause states lifetime bounds after the result type, or after the parameter list when the result annotation is omitted. See lifetime parameters.

Ordinary parameters use IdentifierBinding followed by an explicit type. Function signatures determine parameter and return types independently of their callers. An optional trailing comma is permitted after a receiver or the last ordinary parameter. If the return type is omitted, it defaults to the unit type ().

An ordinary parameter binding that collides with a visible unqualified const name is undefined behavior; see the constant-name collision guarantee.

The name lookup rules permit forward calls and mutual recursion. Function names may only serve as call targets; treating a function as a first-class value results in undefined behavior. Call expressions specify argument evaluation and parameter passing; return expressions specify function return values. The program entry point is defined in Program entry.

fn add(left: i32, mut right: i32) -> i32 {
    right += left;
    right
}

Receivers

Within an inherent impl, the first parameter may be SelfParam. Its forms are:

FormReceiver
selfBy-value receiver
mut selfMutable by-value receiver
&self or &'a selfShared reference
&mut self or &'a mut selfMutable reference

A function with a receiver is a method; a function without a receiver is an associated function. Any subsequent parameters must follow the FunctionParam syntax. Receiver lifetimes follow the lifetime validity rules, and by-value receivers follow copy/move semantics.