- 📘 Day 4 - Functions
Welcome to Day 4 of the 30 Days of Rust! 🎉 Today, we are going to learn about Functions. Functions are fundamental building blocks in Rust and enable you to organize your code into reusable pieces. Let’s get started! 🚀
Congratulations! 🎉 You've taken the first step in your journey to master the 30 Days of Rust programming challenge. In this challenge, you will learn the fundamentals of Rust and how to harness its power to write efficient, fast, and safe code. By the end of this journey, you'll have gained a solid understanding of Rust's core concepts and best practices, helping you become a confident Rustacean. 🦀
Feel free to join the 30 Days of Rust community on Discord, where you can interact with others, ask questions, and share your progress!
In Rust, functions:
- Allow you to encapsulate code into reusable components.
- Make it easier to understand and manage the flow of logic in your program.
- Can accept parameters, return values, and even call themselves recursively.
We will cover:
- How to define and call functions
- Passing parameters and returning values
- Nested functions, recursion, and more
Ensure that you have your Rust environment set up correctly from Day 1. If you haven’t installed Rust yet, please refer to the setup instructions from Day 1.
Functions in Rust are defined using the fn
keyword followed by the function name and parentheses.
Example:
fn greet() {
println!("Hello, Hunterdii!");
}
fn main() {
greet();
}
Output:
Hello, Hunterdii!
You can pass values to functions as parameters, allowing you to create flexible and reusable functions.
Example:
fn greet_user(name: &str) {
println!("Hello, {}!", name);
}
fn main() {
greet_user("Het");
}
Output:
Hello, Het!
Functions can return values using the ->
symbol followed by the return type.
Example:
fn add(a: i32, b: i32) -> i32 {
a + b
}
fn main() {
let sum = add(5, 3);
println!("The sum is: {}", sum);
}
Output:
The sum is: 8
You can define functions inside other functions, known as nested functions. This is useful when you need helper functions that are only relevant within the parent function.
Example:
fn outer_function() {
fn inner_function() {
println!("This is an inner function.");
}
inner_function();
}
fn main() {
outer_function();
}
Output:
This is an inner function.
Rust can return multiple values using tuples.
Example:
fn calculate(a: i32, b: i32) -> (i32, i32) {
(a + b, a * b)
}
fn main() {
let (sum, product) = calculate(4, 5);
println!("Sum: {}, Product: {}", sum, product);
}
Output:
Sum: 9, Product: 20
A function that calls itself is called a recursive function. Make sure to include a base condition to avoid infinite loops.
Example:
fn factorial(n: u32) -> u32 {
if n == 0 {
1
} else {
n * factorial(n - 1)
}
}
fn main() {
let result = factorial(5);
println!("Factorial of 5 is: {}", result);
}
Output:
Factorial of 5 is: 120
Write a program that:
- Defines a function to check if a number is even or odd.
- Includes a function to calculate the area of a circle given its radius.
- Uses a recursive function to find the greatest common divisor (GCD) of two numbers.
- Write a function that takes a string and returns its length.
- Create a function that returns the maximum of two numbers.
- Define a function to convert Celsius temperatures to Fahrenheit.
- Write a function to print the multiplication table of a given number.
- Implement a function that takes two integers and returns their greatest common divisor (GCD).
- Write a recursive function to find the nth Fibonacci number.
- Create a function that takes a list of numbers and returns the average.
- Implement a function that accepts a string and returns the number of vowels in it.
- Write a program that simulates a calculator with functions for addition, subtraction, multiplication, and division.
- Implement a function to sort an array of integers using the bubble sort algorithm.
- Write a function that uses nested functions to calculate the area and circumference of a circle.
- Rust Functions & Recursion (Hindi)
- Functions in Rust - Rust Programming Tutorial
- Understanding Functions & Parameters in Rust
- Learned how to define and call functions in Rust.
- Explored passing parameters and returning values from functions.
- Understood how to work with nested functions, recursion, and multiple return values.
🌟 Great job on completing Day 4! Keep practicing, and get ready for Day 5 where we will explore Ownership and Borrowing in Rust!
Thank you for joining Day 4 of the 30 Days of Rust challenge! If you found this helpful, don’t forget to star this repository, share it with your friends, and stay tuned for more exciting lessons ahead!
Stay Connected
📧 Email: Hunterdii
🐦 Twitter: @HetPate94938685
🌐 Website: Working On It(Temporary)