0
0
Rustprogramming~15 mins

While loop in Rust - Mini Project: Build & Apply

Choose your learning style9 modes available
Counting Down with a While Loop in Rust
๐Ÿ“– Scenario: Imagine you have a countdown timer for a game or event. You want to count down from a starting number to zero, showing each number as it goes down.
๐ŸŽฏ Goal: You will create a Rust program that uses a while loop to count down from a number to zero and print each number.
๐Ÿ“‹ What You'll Learn
Create a variable called count with the starting number 5
Create a variable called step with the value 1
Use a while loop that runs while count is greater than or equal to 0
Inside the loop, print the current value of count
Inside the loop, decrease count by step
Print the final message "Countdown finished!" after the loop ends
๐Ÿ’ก Why This Matters
๐ŸŒ Real World
Countdown timers are used in games, cooking timers, and event reminders to show time left.
๐Ÿ’ผ Career
Understanding loops and conditions is essential for programming tasks like automation, game development, and system monitoring.
Progress0 / 4 steps
1
Create the countdown starting number
Create a variable called count and set it to the number 5.
Rust
Need a hint?

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

2
Add the step variable
Add a variable called step and set it to 1 inside the main function, below count.
Rust
Need a hint?

Use let step = 1; to create a fixed step size.

3
Write the while loop to count down
Write a while loop that runs while count is greater than or equal to 0. Inside the loop, print the value of count and then subtract step from count.
Rust
Need a hint?

Use while count >= 0 { ... } and inside print with println!("{}", count); then subtract with count -= step;.

4
Print the final message after the loop
After the while loop, write a println! statement to print "Countdown finished!".
Rust
Need a hint?

Use println!("Countdown finished!"); after the loop.