What if you could tell your computer to do boring repetitive tasks for you, perfectly every time?
Why For loop in Rust? - Purpose & Use Cases
Imagine you have a list of 10 names and you want to greet each person one by one. Doing this manually means writing a greeting line for each name separately.
This manual way is slow and boring. If the list grows to 100 or 1000 names, writing each greeting by hand is impossible and full of mistakes.
A for loop lets you tell the computer: "Repeat this greeting for every name in the list." It does the work quickly and without errors, no matter how long the list is.
println!("Hello, Alice!"); println!("Hello, Bob!"); println!("Hello, Carol!");
for name in names { println!("Hello, {}!", name); }
With a for loop, you can easily repeat tasks for many items, making your code shorter, faster, and less error-prone.
Think about sending a thank-you email to everyone who attended your party. Instead of writing each email separately, a for loop helps you send them all with one simple instruction.
Manual repetition is slow and error-prone.
For loops automate repeating tasks over collections.
They make your code cleaner and scalable.