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 tutorialsOpen in YouTube
Duration
6:55
Published
December 21, 2025
Added to Codegiz
March 15, 2026