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.
Practice
for loop do in Rust?Solution
Step 1: Understand the purpose of a for loop
A for loop runs code repeatedly for each item in a list or range.Step 2: Match the description to the options
Only Repeats actions for each item in a collection describes repeating actions over items in a collection.Final Answer:
Repeats actions for each item in a collection -> Option CQuick Check:
For loop = repeat over items [OK]
- Confusing for loop with function definition
- Thinking for loop declares variables
- Believing for loop stops program
Solution
Step 1: Recall Rust for loop syntax
Rust usesfor variable in range { }with ranges like1..=3to include 3.Step 2: Check each option
for i in 1..=3 { println!("{}", i); } uses correct Rust syntax with inclusive range. Others use invalid syntax or functions not in Rust.Final Answer:
for i in 1..=3 { println!("{}", i); } -> Option BQuick Check:
Rust for loop usesinand ranges [OK]
- Using 'to' instead of 'in' keyword
- Using parentheses around loop variable
- Using Python or other language range syntax
let mut sum = 0;
for i in 1..4 {
sum += i;
}
println!("{}", sum);Solution
Step 1: Identify the loop range and iterations
The loop runs for i = 1, 2, 3 because 1..4 excludes 4.Step 2: Calculate the sum of iterations
sum = 0 + 1 + 2 + 3 = 6Final Answer:
6 -> Option AQuick Check:
1+2+3 = 6 [OK]
- Including 4 in the sum
- Starting sum at 1 instead of 0
- Confusing inclusive and exclusive ranges
for i in 0..5
println!("{}", i);
}Solution
Step 1: Check loop syntax
Rust requires curly braces { } around the loop body if it has more than one statement or to be explicit.Step 2: Identify the missing brace
The code has a closing brace '}' without an opening brace '{' before println!().Final Answer:
Missing curly braces around loop body -> Option DQuick Check:
For loop body needs braces [OK]
- Forgetting opening brace
- Assuming range syntax is wrong
- Thinking variable needs declaration outside loop
Solution
Step 1: Understand the range and condition
We want numbers from 2 to 10 inclusive, so use 2..=10. To print even numbers, check if remainder when divided by 2 is zero.Step 2: Check each option's condition
for i in 2..=10 { if i % 2 == 0 { println!("{}", i); } } correctly usesi % 2 == 0. for i in 2..10 { if i % 2 = 0 { println!("{}", i); } } uses assignment (=) instead of comparison (==). for i in 2..=10 { if i / 2 == 0 { println!("{}", i); } } uses division instead of modulo. for i in 2..=10 { if i % 2 != 0 { println!("{}", i); } } prints odd numbers.Final Answer:
for i in 2..=10 { if i % 2 == 0 { println!("{}", i); } } -> Option AQuick Check:
Use modulo == 0 for even check [OK]
- Using '=' instead of '==' in condition
- Using division instead of modulo
- Printing odd numbers by mistake
Ready to prove it?
Pass the test and this pattern moves unknown
