Rust Loops Tutorial | loop while for for Beginners (2024)
5views
1likes
06:55
T
Taught by Celeste AI - AI Coding Coach
View on YouTubeDescription
Learn all three types of loops in Rust with a relaxed, beginner-friendly pace! This tutorial covers loop with break, while loops with conditions, and for loops with ranges.
๐ฅ In This Tutorial:
โโโโโโโโโโโโโโโโโโโโโโโ
0:00 - Introduction
0:11 - Basic loop with break
2:30 - While Loop (Countdown Timer)
4:53 - For Loop with Range
๐ What You'll Learn:
โโโโโโโโโโโโโโโโโโโโโโโ
โ
The infinite 'loop' keyword and how to break out
โ
While loops that check conditions each iteration
โ
For loops with range syntax (1..6 vs 1..=5)
โ
Using 'mut' for mutable variables
โ
When to use each loop type
๐ป Code Examples:
โโโโโโโโโโโโโโโโโโโโโโโ
Example 1 - Basic Loop with Break:
fn main() {
let mut count = 0;
loop {
count = count + 1;
println!("Count is: {}", count);
if count == 5 {
break;
}
}
}
Example 2 - While Loop Countdown:
fn main() {
let mut seconds = 5;
while seconds is greater than 0 {
println!("{} seconds remaining...", seconds);
seconds = seconds - 1;
}
println!("Liftoff!");
}
Example 3 - For Loop with Range:
fn main() {
for number in 1..6 {
println!("Number: {}", number);
}
}
// Note: 1..6 gives 1,2,3,4,5 (6 is excluded)
// Use 1..=5 for inclusive range
๐ฆ Key Takeaways:
โโโโโโโโโโโโโโโโโโโโโโโ
โข loop runs forever until break is called
โข while checks a condition before each iteration
โข for is best for iterating over ranges
โข 1..6 excludes the end, 1..=5 includes it
โข Use 'mut' when you need to change a variable
๐ Prerequisites:
โโโโโโโโโโโโโโโโโโโโโโโ
โข Rust installed (rustc --version)
โข Basic understanding of Rust syntax
โข Watch our previous Rust tutorials!
๐ Like, Subscribe & Share for more Rust tutorials!
#Rust #RustLang #Programming #Loops #ForLoop #WhileLoop #Coding #Tutorial #LearnRust #Beginner #rustlang2024
Back to tutorials๐ Read the articleOpen in YouTube
Duration
6:55
Published
December 21, 2025
Added to Codegiz
March 15, 2026