0
0
Rustprogramming~10 mins

Using if as expression in Rust - Mini Project: Build & Apply

Choose your learning style9 modes available
Using if as expression
๐Ÿ“– Scenario: You are building a simple program to decide if a person is an adult or a minor based on their age.
๐ŸŽฏ Goal: Create a Rust program that uses if as an expression to assign a message depending on the age.
๐Ÿ“‹ What You'll Learn
Create a variable age with the value 20
Create a variable status that uses if as an expression to assign "Adult" if age is 18 or more, otherwise "Minor"
Print the status variable
๐Ÿ’ก Why This Matters
๐ŸŒ Real World
Using <code>if</code> as an expression helps write concise code for decisions like checking age limits.
๐Ÿ’ผ Career
Understanding expressions and conditional assignments is important for writing clean and efficient Rust code in real projects.
Progress0 / 4 steps
1
Create the age variable
Create a variable called age and set it to 20.
Rust
Need a hint?

Use let age = 20; to create the variable.

2
Create the status variable using if as expression
Create a variable called status that uses if as an expression to assign "Adult" if age is 18 or more, otherwise assign "Minor".
Rust
Need a hint?

Use let status = if age >= 18 { "Adult" } else { "Minor" };.

3
Print the status variable
Add a println! statement to print the status variable.
Rust
Need a hint?

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

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

The program prints Adult because age is 20.