0
0
Rustprogramming~15 mins

Loop with break in Rust - Mini Project: Build & Apply

Choose your learning style9 modes available
Loop with break
๐Ÿ“– Scenario: You are writing a simple Rust program that counts numbers starting from 1. You want to stop counting once you reach a certain number.
๐ŸŽฏ Goal: Build a Rust program that uses a loop to count numbers starting from 1 and stops the loop using break when the count reaches 5.
๐Ÿ“‹ What You'll Learn
Create a variable called count and set it to 1
Create a variable called max_count and set it to 5
Use a loop to repeatedly print the current count
Inside the loop, use if and break to stop the loop when count is greater than or equal to max_count
Increase count by 1 each time the loop runs
Print the final message "Counting finished!" after the loop ends
๐Ÿ’ก Why This Matters
๐ŸŒ Real World
Loops with break are used in programs to repeat tasks until a certain condition is met, like waiting for user input or processing data until done.
๐Ÿ’ผ Career
Understanding loops and break statements is essential for writing efficient and controlled code in many programming jobs.
Progress0 / 4 steps
1
Create the initial count variable
Create a variable called count and set it to 1.
Rust
Need a hint?

Use let mut count = 1; to create a variable that can change.

2
Add the max_count variable
Add a variable called max_count and set it to 5 below the count variable.
Rust
Need a hint?

Use let max_count = 5; to set the stopping number.

3
Create the loop with break
Use a loop to repeatedly print the current count. Inside the loop, use if count >= max_count and break to stop the loop. Increase count by 1 each time the loop runs.
Rust
Need a hint?

Use loop { ... } and inside it print count. Use if count >= max_count { break; } to stop. Increase count by 1 each time.

4
Print the final message after the loop
After the loop ends, print "Counting finished!".
Rust
Need a hint?

Use println!("Counting finished!"); after the loop to show the final message.