Functions are reusable code that runs during execution.

Rust Functions

Function definition contains header and body.

Here is a function definition

fn function_name(arguments)-> datatype{
    // body
}

fn function_name(arguments)-> datatype is Function header code inside {} is called function body.

fn is a keyword, used to create a function.

function_name is the name of the function, it is snake_case arguments: data can be passed to the function in the form of arguments. Multiple arguments are specified by datatype and argument separated by a comma. datatype: Functions return the data, You need to specify what type of data it returns.

Once the function is declared, You need to call the function with the below syntax. Syntax:

function_name(arguments);

It provides reusable code and is called in many places. Here is a simple function example in Rust.

fn main() {
    display_message();
}

fn display_message() {
    println!("Welcome to function tutorials in Rust")
}

Output:

Welcome to function tutorials in Rust.

Function arguments example

The functions header contains arguments. Arguments are separated by a comma(,). Each argument can be declared with a datatype and variable. These variables are local to the Function.

Here is a Rust function arguments example program.

fn main() {
    let name = "John".to_string();
    display_message(name, 5000);
}

fn display_message(name: String, salary: u32) {
    println!("{} - {}", name, salary)
}

Output:

John - 5000

Function Return single and multiple values

Functions always return a single value. The function header contains -> followed by the data type. The function body contains the return keyword if there is a datatype declared.

It is optional to have a returning value.

Here is a Function return single values example.

fn main() {
    println!("Sum: {}", add(20, 50));
}

fn add(first: u32, second: u32) -> u32 {
    let result = first + second;
    return result;
}

Output:

Sum: 70

Let’s see another Function example to return multiple values

The function always returns a single value and a single type.

You can use tuples, arrays, and vectors to return multiple values. Let’s use a tuple to have a single type for holding multiple values and types.

Here is a Rust Function example to return tuple type

fn main() {
    let employee: (u32, &str, bool, u32) = (1, "John", false, 5000);

    let emp = get_employee(employee);
    println!("{:?}", emp);
}
fn get_employee(emp: (u32, &str, bool, u32)) -> (u32, &str, bool, u32) {
    println!("{:#?}", emp);
    return emp;
}

Rust anonymous Functions or closures

The above sections have a function with a name. You can declare a function without the name, also called an anonymous functions.

Anonymous functions are also called Closure in Rust. These functions are assigned to variables.

Anonymous Functions syntax

let name=|arguments|-> datatype{body}

These functions are called using the below syntax

name(arguments);

Let’s see an example of anonymous functions.

  • Anonymous Functions without arguments || Syntax is used to declare anonymous functions without arguments.

Here is an example

fn main() {
    let display = || {
        println!("Anonymous Functions without arguments");
    };
    display();
}

Output:

Anonymous Functions without arguments
  • Anonymous Functions with arguments These functions can be passed and declared data type with a return inside a body. Here is an example.
fn main() {
    let add = |first: u32, second: u32| -> u32 {
        println!("Anonymous Functions with arguments");
        let result = first + second;
        return result;
    };
    println!("{}", add(3, 1));
}

Output:

Anonymous Functions with arguments
4