Skip to main content

Rust Functions

Hemlata
EditReport

Rust Functions

Functions are reusable blocks of code used to perform a specific task.

Functions help make programs:

  • Cleaner
  • Reusable
  • Easy to understand
  • Easy to maintain

In Rust, the main() function is the starting point of every program.


Basic Function Syntax

Functions are created using the fn keyword.

Syntaxโ€‹

fn function_name() {
// code
}

Exampleโ€‹

fn greet() {
println!("Welcome to Rust!");
}

fn main() {
greet();
}

Outputโ€‹

Welcome to Rust!

Function Parameters

Functions can accept values called parameters.

Parameters are written inside parentheses.

Exampleโ€‹

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

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

Outputโ€‹

Hello, Hemlata

Multiple Parameters

A function can have multiple parameters.

Exampleโ€‹

fn add(a: i32, b: i32) {
println!("Sum: {}", a + b);
}

fn main() {
add(10, 20);
}

Outputโ€‹

Sum: 30

Function Return Values

Functions can return values using the return type syntax ->.

Exampleโ€‹

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

fn main() {
let result = square(5);

println!("Square: {}", result);
}

Outputโ€‹

Square: 25

In Rust, the last expression without a semicolon is automatically returned.


Using the return Keyword

Rust also allows returning values using the return keyword.

Exampleโ€‹

fn multiply(a: i32, b: i32) -> i32 {
return a * b;
}

fn main() {
let result = multiply(4, 5);

println!("Result: {}", result);
}

Outputโ€‹

Result: 20

Functions Without Return Value

Functions that do not return anything have a return type of () called the unit type.

Exampleโ€‹

fn message() {
println!("This function returns nothing.");
}

fn main() {
message();
}

Function with Boolean Return Type

Functions can also return boolean values.

Exampleโ€‹

fn is_even(num: i32) -> bool {
num % 2 == 0
}

fn main() {
println!("{}", is_even(10));
println!("{}", is_even(7));
}

Outputโ€‹

true
false

Calling Functions Multiple Times

Functions can be reused multiple times in a program.

Exampleโ€‹

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

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

Outputโ€‹

Hello from Rust!
Hello from Rust!
Hello from Rust!

Function Expressions

The last line of a function can act as an expression.

Exampleโ€‹

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

fn main() {
let result = sum(3, 7);

println!("{}", result);
}

Outputโ€‹

10

Nested Function Calls

One function can call another function.

Exampleโ€‹

fn display() {
println!("Display Function");
}

fn show() {
display();
println!("Show Function");
}

fn main() {
show();
}

Outputโ€‹

Display Function
Show Function

Function Ownership Example

Rust follows ownership rules even in functions.

Exampleโ€‹

fn print_text(text: String) {
println!("{}", text);
}

fn main() {
let message = String::from("Rust");

print_text(message);
}

After passing message, ownership moves to the function.


Mutable Parameters Example

Functions can work with mutable values.

Exampleโ€‹

fn main() {
let mut number = 10;

increase(&mut number);

println!("{}", number);
}

fn increase(num: &mut i32) {
*num += 1;
}

Outputโ€‹

11

Advantages of Functions

Functions provide many benefits:

  • Code reusability
  • Better readability
  • Easier debugging
  • Organized programs
  • Reduced code duplication

Summary

Functions are one of the most important parts of Rust programming.

Important Pointsโ€‹

  • Functions are declared using fn
  • Parameters allow passing values
  • Functions can return values
  • Rust supports expression-based returns
  • Functions improve code organization and reusability

Functions help developers write clean, modular, and efficient Rust programs.

Finished reading? Mark this topic as complete.