03_control_flow
π¦ 30 Days Of Rust: Day 3 - Control Flow π
Author: Het Patel
October, 2024
π Day 3 - Control Flow
π Welcome
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!
π Overview
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 ifstatementsVarious loop mechanisms:
loop,while, andforThe powerful
matchstatement for pattern matching
π Environment Setup
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.
π Control Flow in Rust
π§© The if Statement
if StatementThe 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π The else and else if Statements
else and else if StatementsUse 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
loop StatementThe 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
while LoopThe 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
for LoopThe 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π Control Flow with match
matchThe 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π― Hands-On Challenge
Write a program that:
Asks the user to input a number.
Uses an
ifstatement to check if the number is even or odd.Use a
loopto print numbers from 1 to 5.Implement a
matchstatement to respond to different days of the week, e.g., "Monday" => "Start of the week!", "Friday" => "Weekend is coming!", etc.
π» Exercises - Day 3
β
Exercise: Level 1
Write a program that checks if a number is even or odd using the
ifstatement.Create a
whileloop that prints numbers from 1 to 10.Use the
forloop to iterate over an array of your favorite colors and print each one.Create a simple calculator using the
matchstatement 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.
β
Exercise: Level 2
Create a program that calculates the factorial of a given number using a
whileloop.Write a program that simulates a countdown timer using a
loopand breaks when the countdown reaches zero.Use the
forloop to calculate the sum of even numbers from 1 to 50.Write a program that reads a string input and uses the
matchstatement to respond with different outputs based on the input (e.g., "hello" => "Hi there!", "bye" => "Goodbye!", etc.).Implement a program that uses
ifstatements inside aforloop 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
loopto keep asking until the user gets it right.
π₯ Helpful Video References
π Day 3 Summary
Learned about using
if,else, andelse iffor conditional checks.Explored
loop,while, andforloops to repeat actions.Discovered the
matchstatement 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)
Last updated