Mastering Rust Functions (For Beginners)

In Rust, functions are declared using the fn keyword. They take zero or more parameters, may return a value, and are used to organize reusable blocks of logic. Every Rust program starts with a special function named main.

Functions help you organize your code and make it reusable. In Rust, defining a function is simple and always starts with the fn keyword. You can pass in values, return results, and use type annotations to keep everything clear and safe.

In this post, I will show you how to write functions in Rust, how to use parameters, return values, and how function syntax compares to what you may be used to in JavaScript/Node.js.

Writing a Basic Function in Rust

Here is the simplest kind of function:

fn greet() {
    println!("Hello!");
}

To run this function, call it from main:

fn main() {
    greet();
}

The fn keyword starts the function, greet is the name, and () means it takes no arguments. Inside the curly braces is the body of the function.

Function with Parameters

You can pass values to functions by defining parameters with types:

fn greet(name: &str) {
    println!("Hello, {}", name);
}

fn main() {
    greet("Alice");
}

This function takes a name and prints it. The &str means it accepts a string slice.

Multiple Parameters:

You can define functions with more than one parameter. Each parameter must have a name and type:

fn print_info(name: &str, age: u8) {
    println!("Name: {}, Age: {}", name, age);
}

When calling it, make sure the values match the types exactly.

Returning Value from a Function

Functions in Rust can return values. The return type is shown with -> after the parameter list:

fn add(a: i32, b: i32) -> i32 {
    a + b
}

There is no return keyword here because Rust can return the last line of the function automatically, as long as it does not have a semicolon.

You can also use return explicitly:

fn multiply(x: i32, y: i32) -> i32 {
    return x * y;
}

Functions Return a Single Type:

Functions in Rust can return only one type. If you want to return multiple values, use a tuple:

fn calculate(x: i32, y: i32) -> (i32, i32) {
    (x + y, x * y)
}

Then unpack it like this:

let (sum, product) = calculate(4, 5);

Calling Functions

You call functions using their name and parentheses:

fn main() {
    let result = add(5, 3);
    println!("Result: {}", result);
}

This prints Result: 8.

Function Naming

Use snake_case for function names:

fn get_user_name() {  // correct
    // ...
}

fn getUserName() {   // wrong - not idiomatic Rust
    // ...
}

This is the standard style in Rust.

Example Program

fn square(n: i32) -> i32 {
    n * n
}

fn main() {
    let number = 6;
    let result = square(number);

    println!("The square of {} is {}", number, result);
}

This program defines a reusable square function and uses it to compute and print a value.

Summary

Functions in Rust are declared with fn, can take parameters with types, and can return a value using ->. You can use them to group code and make your programs easier to understand and reuse. Rust makes you be clear about what your function expects and what it returns, which helps avoid bugs.