Functions in Rust
In this lesson, you will learn how to define and use functions in Rust. Functions allow you to organize code into reusable blocks and improve readability and maintainability.
Rust functions are clear, predictable, and strongly typed, making programs safer and easier to understand.
What Is a Function?
A function is a block of code that performs a specific task. Instead of writing the same logic repeatedly, you can define a function once and call it whenever needed.
Functions help in:
- Reducing code duplication
- Improving readability
- Breaking large programs into smaller parts
- Making code easier to test and maintain
Defining a Function
In Rust, functions are defined using the fn keyword.
The basic syntax of a function looks like this:
fn function_name() {
// function body
}
The function name should describe what the function does.
A Simple Function Example
Here is a simple function that prints a message.
fn greet() {
println!("Hello from Rust!");
}
fn main() {
greet();
}
The greet() function is defined once and called inside main().
Function Parameters
Functions can accept input values called parameters. These values are passed to the function when it is called.
In Rust, you must explicitly specify the type of each parameter.
fn print_number(x: i32) {
println!("The number is {}", x);
}
fn main() {
print_number(10);
}
Here, x is a parameter of type i32.
Multiple Parameters
A function can take more than one parameter. Parameters are separated by commas.
fn add(a: i32, b: i32) {
println!("Sum is {}", a + b);
}
fn main() {
add(5, 3);
}
Return Values
Functions can return values to the caller.
In Rust, the return type is specified after an arrow ->.
fn square(x: i32) -> i32 {
x * x
}
fn main() {
let result = square(4);
println!("Result: {}", result);
}
Notice that the last expression does not end with a semicolon. This makes it the return value.
Using return Keyword
You can also use the return keyword to return a value explicitly.
fn multiply(a: i32, b: i32) -> i32 {
return a * b;
}
fn main() {
let value = multiply(3, 4);
println!("{}", value);
}
Functions Without Return Values
If a function does not return a value, its return type is implicitly (),
which is called the unit type.
fn display_message() {
println!("No return value here");
}
Calling Functions Multiple Times
Once defined, a function can be called any number of times.
fn greet() {
println!("Welcome!");
}
fn main() {
greet();
greet();
greet();
}
Why Functions Matter in Rust
Functions are the foundation of clean Rust programs. They help enforce clear logic and strong typing.
Most Rust applications heavily rely on functions for modular design.
📝 Practice Exercises
Exercise 1
Create a function that prints your name.
Exercise 2
Write a function that takes two numbers and prints their sum.
Exercise 3
Create a function that returns the square of a number.
Exercise 4
Write a function that returns a boolean value.
✅ Practice Answers
Answer 1
fn print_name() {
println!("Dataplexa");
}
Answer 2
fn sum(a: i32, b: i32) {
println!("{}", a + b);
}
Answer 3
fn square(n: i32) -> i32 {
n * n
}
Answer 4
fn is_even(n: i32) -> bool {
n % 2 == 0
}
What’s Next?
Now that you understand functions, you are ready to learn Rust’s most important concept — ownership.
In the next lesson, you will explore ownership in Rust.