Rust Functions

Rust Tutorial

Rust Functions – Complete Beginner Guide

Functions are the building blocks of every Rust program.

They allow you to:

  • Organize code

  • Reuse logic

  • Improve readability

  • Control ownership and borrowing

  • Build modular applications

In this fully beginner guide, you’ll learn:

  • What functions are in Rust

  • How to define and call functions

  • Parameters and arguments

  • Return values

  • Expression vs statement

  • Ownership and borrowing in functions

  • Advanced function features

  • Best practices

Let’s dive in


What Is a Function in Rust?

A function is a block of reusable code that performs a specific task.

In Rust, functions are defined using the fn keyword.

Basic example:


 

Here:

  • fn defines a function

  • main() is the entry point of every Rust program


Basic Function Syntax


 

Example:


 

Calling it:


 


Function Parameters in Rust

Functions can take input values called parameters.


 

Important:

Rust requires you to specify parameter types.


Multiple Parameters


 

Rust enforces type safety strictly.


Returning Values from Functions

In Rust, you must specify the return type using ->.


 

Notice:

  • No return keyword needed

  • No semicolon at the end

Rust returns the last expression.


Expression vs Statement (Very Important)

In Rust:

  • Expressions return values

  • Statements do not

Correct:


 

Wrong:


 

Semicolons change behavior.


Using return Keyword

You can also write:


 

But idiomatic Rust prefers expression style.


Visual Flow of Function Execution

Visual Flow of Function Execution

Functions execute:

  1. Parameters received

  2. Code runs

  3. Value returned


Ownership in Functions

Functions follow ownership rules.

Example:


 

Ownership moved into function.


Borrowing in Functions

To avoid moving ownership:


 

Now original value remains usable.

Best practice: use &str instead of &String.


Mutable References in Functions


 

  •  Allows modification
  •  Only one mutable reference allowed

Functions and Scope

Variables created inside functions:

  • Exist only inside that function

  • Are dropped when function ends


 

x is not accessible outside.


Functions with Conditional Returns


 

Better version:


 

Rust encourages concise expressions.


Returning Multiple Values (Using Tuples)


 

Rust does not support multiple return values directly, but tuples solve this.


Function Pointers

You can assign functions to variables.


 


Closures (Short Functions)

Closure are anonymous functions.


 

Closures are powerful in Rust.


Inline Functions

Rust does not guarantee inlining, but you can suggest:


 

Used for performance optimization.


Associated Functions (Methods)

Functions inside impl blocks:


 

Called using:


 


Main Function Special Rules

  • Must be named main

  • No parameters

  • Can return Result

Example:


 

Useful for error handling.


Common Beginner Mistakes

  •  Forgetting return type
  • Adding semicolon at end of return expression
  • Moving ownership unintentionally
  •  Forgetting parameter types
  • Confusing functions and closures

Best Practices

  •  Prefer expression-style returns
  •  Use borrowing instead of moving when possible
  •  Keep functions small and focused
  • Use descriptive names
  • Use &str for string parameters

Frequently Asked Questions (FAQs)

1. What is a function in Rust?

A function is a reusable block of code defined using the fn keyword.


2. How do you return a value in Rust?

You specify return type using -> and return the final expression without a semicolon.


3. Does Rust require type annotations in functions?

Yes. Parameter and return types must be explicitly declared.


4. What happens to variables inside a function?

They go out of scope and are dropped when the function ends.


5. What is the difference between a function and a closure?

Functions are defined using fn. Closures are anonymous and can capture variables from their environment.


Conclusion

Rust functions are:

  • Strictly typed

  • Expression-based

  • Ownership-aware

  • Memory safe

Understanding functions helps you master:

  • Ownership

  • Borrowing

  • Scope

  • Modular design

Once you’re comfortable with Rust functions, writing structured programs becomes much easier.

You may also like...