What Are The Function Parameters In Rust?

Parameters are a powerful feature that allows you to pass data to a function to make the code reusable.

Learn here, What are the Functions in Rust?

We can tell the function what kind of input it will receive by using parameters. When we call the function, we give actual values (arguments), which are passed to the parameters.

Example:

fn say_hello(name: &str) {
println!("Hello, {}!", name);
}
  • We learned in a previous topic, how to define a function using “fn”.
  • say_hello is the function name in the above code.
  • name: &str is the parameter, meaning the function expects to receive a string slice ($str), and a name is just a variable that holds the value temporarily.
  • ( {} ) It uses the value inside the functions.
say_hello("John");
  • “John” is the argument.
  • It’s passed to the parameter name.
  • Final output will be:
Hello, John!

Rules About Parameters in Rust

  1. Parameters must have explicit types.
  2. You can define zero or more parameters.
  3. Rust functions use comma-separated lists for multiple parameters.
  4. Rust supports reference, ownership, and borrowing to understand how parameters work with memory.

Simple Example1

We create two parameters to pass the values.

fn greet_user(name: &str, age: u8) {
println!("Hello, {}! You are {} years old.", name, age);
}

fn main() {
greet_user("Riya", 21);
}

Output:

Hello, Riya! You are 21 years old.

This function takes two parameters: a string and an integer. this code explains how multiple parameters are passed and used.

Simple Example 2

We create a simple mini project that will calculate simple interest.

fn calculate_interest(principal: f64, rate: f64, time: f64) -> f64 {
(principal * rate * time) / 100.0
}

fn main() {
let interest = calculate_interest(1000.0, 5.0, 2.0);
println!("Simple Interest = {}", interest);
}

Output:

Simple Interest = 100

In this code, we pass three f64 parameters to a function and return a calculated result.

Function Parameters vs Arguments in Rust

These two terms confuse beginners, but they have different roles in functions.

a) Parameter

A parameter is a variable in a function definition. It is a named variable you define inside the function, so the function knows what kind of input it will get.

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

a and b are parameters, and they are variables of type i32. They don’t have any values yet. It just defines what kind of data the function expects.

b) Argument

On the other hand, an argument is an actual value that you pass when you call the function.

fn main() {
let result = multiply(3, 4);
println!("Result: {}", result);
}

Here, 3 and 4 are arguments, and these values are passed into the parameters a and b.

Example 3: Parameter as Boolean

fn is_eligible(age: u8, has_id: bool) {
if age >= 18 && has_id {
println!("You are eligible to enter.");
} else {
println!("Access denied.");
}
}

fn main() {
is_eligible(20, true);
}

Output:

You are eligible to enter.

This example shows how a Boolean parameter can change function logic.

How to Pass a Structs as Parameters?

Rust allows us to pass complex data types, like structs, as a parameter. it is a common pattern that works with structured or grouped data.

Learn this example carefully:

struct User {
name: String,
email: String,
}

fn print_user(user: &User) {
println!("Name: {}, Email: {}", user.name, user.email);
}

fn main() {
let u1 = User {
name: String::from("Joy"),
email: String::from("Joy@example.com"),
};
print_user(&u1);
}

Output of this program:

Name: Joy, Email: Joy@example.com
  • This code creates a struct named User with two fields: Name and Email, which both hold a String.
  • Then we created a variable u1 of type user.
  • String: :from(. . . ) creates new owned String values for name and email.
  • We are calling the print_user function and passing a reference to u1 (&u1) so the function can read its data.

A struct is a custom data type that groups multiple pieces of data under one name.

Practice Exercise for You

Task:
Create a function that takes two integers and prints whether the first is divisible by the second.

Simple hints of this exercise:

  • Use % operator
  • Accept parameters like a: i32, b: i32
  • Use if condition

Learn other Topics About Rust Programming

Leave a Comment