codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🦀

Rust

11 / 58 topics
11Functions12Closures
Tutorials/Rust/Functions
🦀Rust

Functions

Updated 2026-05-15
10 min read

Functions

In this section, we will delve into the fundamentals of defining and calling functions in Rust. Understanding how to create and use functions is crucial for writing clean, reusable, and maintainable code.

Introduction

Functions are blocks of organized, reusable code that are used to perform a single, related action. They provide better modularity for your application and make your code more readable and manageable. In Rust, functions are defined using the fn keyword followed by the function name and parentheses which may include parameters.

Concept

A function in Rust consists of several parts:

  1. Function Declaration: This includes the fn keyword, the function name, and any parameters enclosed in parentheses.
  2. Return Type: The type of value that the function returns is specified after an arrow (->). If the function does not return a value, you can omit this part or use the unit type ().
  3. Function Body: This is where the actual code resides and is enclosed within curly braces {}.

Here's a basic structure of a function in Rust:

fn function_name(parameters) -> return_type {
    // Function body
    // Code to be executed
}

## Examples

### Basic Function

Let's start with a simple example where we define a function that prints "Hello, World!".

<CodeBlock language="rust">
{`fn main() {
    print_hello();
}

fn print_hello() {
    println!("Hello, World!");
}`}
</CodeBlock>

In this example:
- `main` is the entry point of the program.
- `print_hello` is a function that takes no parameters and returns nothing (`()`).
- The `println!` macro is used to print text to the console.

### Function with Parameters

Next, let's define a function that takes two integers as parameters and prints their sum.

<CodeBlock language="rust">
{`fn main() {
    add_numbers(5, 3);
}

fn add_numbers(a: i32, b: i32) {
    println!("The sum is: {}", a + b);
}`}
</CodeBlock>

In this example:
- `add_numbers` takes two parameters `a` and `b`, both of type `i32`.
- The function prints the sum of `a` and `b`.

### Function with Return Value

Now, let's define a function that returns the product of two numbers.

<CodeBlock language="rust">
{`fn main() {
    let result = multiply_numbers(4, 6);
    println!("The product is: {}", result);
}

fn multiply_numbers(a: i32, b: i32) -> i32 {
    a * b
}`}
</CodeBlock>

In this example:
- `multiply_numbers` takes two parameters `a` and `b`, both of type `i32`.
- The function returns the product of `a` and `b` using the `-> i32` syntax.
- The return value is assigned to the variable `result` in the `main` function.

### Early Return

Rust also allows you to return early from a function using the `return` keyword. However, it's often omitted as Rust automatically returns the last expression in a block.

<CodeBlock language="rust">
{`fn main() {
    let result = check_number(10);
    println!("The result is: {}", result);
}

fn check_number(num: i32) -> String {
    if num > 5 {
        return "Greater than 5".to_string();
    }
    "Less than or equal to 5".to_string()
}`}
</CodeBlock>

In this example:
- `check_number` checks if the input number is greater than 5.
- If true, it returns "Greater than 5".
- Otherwise, it returns "Less than or equal to 5".

## What's Next?

Now that you understand how to define and call functions in Rust, the next step is to explore closures. Closures are similar to functions but can capture their environment and have more flexible usage patterns.

Stay tuned for more advanced topics in Rust programming!

PreviousLoopsNext Closures

Recommended Gear

LoopsClosures