0
0
Rustprogramming~15 mins

If–else expression in Rust - Mini Project: Build & Apply

Choose your learning style9 modes available
Using If-else Expression in Rust
📖 Scenario: You are creating a simple program that decides if a number is positive or not. This is like checking if the temperature is above zero to decide if you need a jacket.
🎯 Goal: Build a Rust program that uses an if-else expression to check if a number is positive and prints the result.
📋 What You'll Learn
Create a variable called number with the value 10
Create a variable called result that uses an if-else expression to check if number is greater than 0
If number is greater than 0, result should be the string "Positive"
Otherwise, result should be the string "Not positive"
Print the value of result
💡 Why This Matters
🌍 Real World
If-else expressions are used in many programs to make decisions, like checking if a user is logged in or if a payment is successful.
💼 Career
Understanding if-else expressions is essential for writing clear and efficient code in Rust and many other programming languages.
Progress0 / 4 steps
1
Create the number variable
Create a variable called number and set it to 10.
Rust
Need a hint?

Use let number = 10; inside the main function.

2
Create the result variable with if-else expression
Create a variable called result that uses an if-else expression to check if number is greater than 0. If yes, set result to "Positive", else set it to "Not positive".
Rust
Need a hint?

Use let result = if number > 0 { "Positive" } else { "Not positive" };.

3
Print the result
Add a println! statement to print the value of result.
Rust
Need a hint?

Use println!("{}", result); to print the result.

4
Run and see the output
Run the program and observe the output. It should print Positive.
Rust
Need a hint?

The program should print Positive because 10 is greater than 0.