Rust Functions

A function is a block of statements that perform a specific task, which can be used multiple times within a program. In Rust, as with a lot of languages, there are a number of functions built in, however, it is also possible to create user defined functions. The statements within a function are only executed once the function is called, so, if the function is never called then they won’t run. In Rust, a function can be defined and called as follows.

// Function definition.
fn function_name(arguments)
{
    // Statement(s) to execute.
}

// Function call.
function_name(arguments);

When a function is defined it always starts with the keyword ‘fn’, followed by the function name and any arguments that need to be passed to it in parenthesis. If there are no arguments to be passed to the function, then the parenthesis still need to be present, but with nothing enclosed. As with variables, function names can contain letters, numbers and underscores, but must start with either a letter or underscore. By convention, function names are in lower case in Rust, with words separated by underscores.

Below is a simple example of a function definition and call, where no arguments are passed to the function, it just prints out the message, ‘Hello World!’.

fn main() {

    // Function call.
    hello();

}

// Function definition.
fn hello() {

    println!("Hello World!");

}

The above example can be enhanced to include arguments, such as a person’s name. Here two arguments of type 'String', first name and last name, are passed to the function, separated by a comma.

fn main() {

    // Function call.
    hello("Fred".to_string(), "Bloggs".to_string());

}

// Function definition.
fn hello(fname: String, sname: String) {

    println!("Hello {} {}!", fname, sname);

}

The resulting output from this is as follows.

Hello Fred Bloggs!

As well as being able to pass one or more arguments into a function, it is also possible for a value to be returned. The example below again takes two arguments, ‘fname’ and ‘sname’, and returns a message string incorporating the two arguments, which is displayed. This again produces the same output as above. Note that, the return type of 'String' must be specified for this to work.

fn main() {

    // Function call within 'println'.
    println!("{}", hello("Fred".to_string(), "Bloggs".to_string()));

}

// Function definition.
fn hello(fname: String, sname: String) -> String {

    return "Hello ".to_owned() + &fname +
           &" ".to_owned() + &sname + &"!".to_owned();

}

If the message being returned from the function is required later on in the program, it could first be assigned to a variable.

fn main() {

    // Function call return value assigned to a variable.
    let greeting = hello("Fred".to_string(), "Bloggs".to_string());

    // Display greeting.
    println!("{}", greeting);

}

// Function definition.
fn hello(fname: String, sname: String) -> String {

    return "Hello ".to_owned() + &fname +
           &" ".to_owned() + &sname + &"!".to_owned();

}