- 📘 Day 3 - Control Flow
Welcome to Day 3 of the 30 Days of Rust! 🎉 Today, we will explore Control Flow in Rust. Understanding control flow is essential for directing the flow of your program based on conditions, repeating actions, or managing various scenarios effectively. Let’s dive into it! 🚀
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, control flow allows you to:
- Make decisions using conditional statements.
- Repeat actions using different types of loops.
- Handle multiple scenarios efficiently.
We will cover:
if
,else
, andelse if
statements- Various loop mechanisms:
loop
,while
, andfor
- The powerful
match
statement for pattern matching
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.
The if
statement lets you execute a block of code only if a specified condition is true.
Example:
fn main() {
let number = 10;
if number > 5 {
println!("The number is greater than 5");
}
}
Output:
The number is greater than 5
Use else
and else if
to add alternative conditions.
Example:
fn main() {
let age = 18;
if age >= 21 {
println!("You can drink alcohol in the INDIA.");
} else if age >= 18 {
println!("You are an adult, but cannot drink alcohol in the INDIA.");
} else {
println!("You are a minor.");
}
}
Output:
You are an adult, but cannot drink alcohol in the INDIA.
The loop
statement repeatedly executes a block of code. To break out, use the break
keyword.
Example:
fn main() {
let mut count = 0;
loop {
count += 1;
if count == 3 {
println!("Breaking the loop at count: {}", count);
break;
}
}
}
Output:
Breaking the loop at count: 3
The while
loop continues to run while a condition is true.
Example:
fn main() {
let mut num = 1;
while num <= 5 {
println!("Number is: {}", num);
num += 1;
}
}
Output:
Number is: 1
Number is: 2
Number is: 3
Number is: 4
Number is: 5
The for
loop is useful for iterating over a collection or a range of numbers.
Example:
fn main() {
for num in 1..4 {
println!("Num: {}", num);
}
}
Output:
Num: 1
Num: 2
Num: 3
The match
statement lets you handle multiple scenarios by pattern matching.
Example:
fn main() {
let traffic_light = "green";
match traffic_light {
"green" => println!("Go"),
"yellow" => println!("Slow down"),
"red" => println!("Stop"),
_ => println!("Invalid color"),
}
}
Output:
Go
Write a program that:
- Asks the user to input a number.
- Uses an
if
statement to check if the number is even or odd. - Use a
loop
to print numbers from 1 to 5. - Implement a
match
statement to respond to different days of the week, e.g., "Monday" => "Start of the week!", "Friday" => "Weekend is coming!", etc.
- Write a program that checks if a number is even or odd using the
if
statement. - Create a
while
loop that prints numbers from 1 to 10. - Use the
for
loop to iterate over an array of your favorite colors and print each one. - Create a simple calculator using the
match
statement that performs addition, subtraction, multiplication, or division based on user input. - Write a program that continuously takes user input until the word "exit" is typed, using a
loop
.
- Create a program that calculates the factorial of a given number using a
while
loop. - Write a program that simulates a countdown timer using a
loop
and breaks when the countdown reaches zero. - Use the
for
loop to calculate the sum of even numbers from 1 to 50. - Write a program that reads a string input and uses the
match
statement to respond with different outputs based on the input (e.g., "hello" => "Hi there!", "bye" => "Goodbye!", etc.). - Implement a program that uses
if
statements inside afor
loop to print all the odd numbers from 1 to 20. - Create a small game where the program generates a random number between 1 and 10, and the user has to guess it. Use a
loop
to keep asking until the user gets it right.
- Control Flow in Rust - Rust Programming Tutorial(Language : Hindi)
- Control Flow Statements & Conditional Expressions(Language : English)
- Rust Loops Explained
- Learned about using
if
,else
, andelse if
for conditional checks. - Explored
loop
,while
, andfor
loops to repeat actions. - Discovered the
match
statement for effective control flow management.
🌟 Great job on completing Day 3! Keep practicing, and get ready for Day 3 where we will explore Functions in Rust!
Thank you for joining Day 3 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)