Table of content

Functions in Rust

Functions in Rust are is a block of statements which helps the user to perform repeated tasks. For example, user wants to perform an operation which takes 5 lines of code. Now when the user wants to perform the same operation in different places, then he/she group this 5 lines of code and the user can call this group whenever required, rather than writing the 5 lines again and again. And this group is called function.

All the Rust programs should start with a function called main() . The program execution, by default, starts from main() function.

fn main() {
    println!("Hello Chercher.tech !");
}

Below are the

There can be two different types of functions in Rust.

  • In-built functions: In-built functions are those which are already inside the library package of Rust and can be called directly. For example: main() itself is an in-built function.
  • User-defined function : User-defined functions are created by the user based on their requirements.

Let us see how simple function can be written in Rust and how it can be invoked or called. Consider the example below:

fn main() {
    println!("Hello Chercher.tech !");
    my_function();
}  
fn my_function() {
    println!("This text is a part of the user-defined function");
}

The output of the program is:

rust-function-execution

The function declaration is done by writing the keyword fn and then the function name.

This rule is the same for the in-built as well as the user-defined function.

Here, in our program my_function() is the user-defined function. Inside the function, we have certain blocks of code.

The function is called inside the main() function by just writing the function name. This function returns NULL and does not have return type.

This is a basic function in Rust.

Function with Single Parameter:

fn main() {
   my_function(5);
}

fn my_function(x: i32) {
    println!("The value of x is: {}", x);
}

As shown in the program, there is a function called my_function() , which has a return type, which is an integer type variable of 32-bit size. This type of function is called a parameterized function.

This is an example of a function with a single parameter.

Function with Multiple Parameter

fn main() {
    my_function(5, 6);
}

fn my_function(x: i32, y: i32) {
    println!("The value of x is: {}", x);
    println!("The value of y is: {}", y);
}

The above program has shown how multiple parameters can be passed within a function. And also only the function definition is sufficient, there no necessity of function declaration separately.

Here, inside the function my_function() two different variable declaration has been made in the parameter field. And, when the function is called inside the main function, the integer variables are automatically referenced inside the function.

This way, not only with two parameters but many parameters can be used inside the function.

Function with Return Type

fn five() -> i32 {
    5  //This is a return value
}

fn main() {
    let x = five();

    println!("The value of x is: {}", x);
}

It may be noted, that in Rust, the last statement will be automatically returned. But you can also return a specific variable, even if it is in the middle of the function. This can be done using the keyword return.

You can just write return variable_name to return the variable whenever you want.

Multiple Statement inside a Variable Declaration

More than one statement can be present inside an explicit Rust Function. Consider the example below:

fn main() {
    let x = 5;

    let y = {
        let x = 3;
        x + 1
    };

    println!("The value of y is: {}", y);
}

In the program, it can be seen clearly, that, the variable y is assigned value only after modifying x.

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions