Rust Loops Tutorial | loop while for for Beginners (2024)

5views
1likes
0
6:55
T
Taught by Celeste AI - AI Coding Coach
View on YouTube
Description
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

Duration

6:55

Published

December 21, 2025

Added to Codegiz

March 15, 2026

๐Ÿ“– Read the articleOpen in YouTube