0
0
Rustprogramming~15 mins

If expression in Rust - Mini Project: Build & Apply

Choose your learning style9 modes available
Using If Expression in Rust
๐Ÿ“– Scenario: Imagine you are building a simple program that decides if a person is old enough to vote. Voting age is 18 years or older.
๐ŸŽฏ Goal: You will create a program that uses an if expression to check a person's age and print whether they can vote or not.
๐Ÿ“‹ What You'll Learn
Create a variable called age with the value 20
Create a variable called voting_eligibility that uses an if expression to check if age is 18 or more
Use if expression to assign "Can vote" if age is 18 or more, otherwise "Cannot vote"
Print the value of voting_eligibility
๐Ÿ’ก Why This Matters
๐ŸŒ Real World
Checking conditions like age eligibility is common in many programs, such as voting systems, age-restricted content, or membership access.
๐Ÿ’ผ Career
Understanding if expressions is fundamental for decision-making in programming, useful in almost every software development job.
Progress0 / 4 steps
1
Create the age variable
Create a variable called age and set it to 20.
Rust
Need a hint?

Use let to create a variable and assign the number 20 to age.

2
Create the voting eligibility variable using if expression
Create a variable called voting_eligibility that uses an if expression to check if age is 18 or more. Assign "Can vote" if true, otherwise "Cannot vote".
Rust
Need a hint?

Use if age >= 18 { "Can vote" } else { "Cannot vote" } to assign the value.

3
Print the voting eligibility
Write a println! statement to print the value of voting_eligibility.
Rust
Need a hint?

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

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

The program should print Can vote because age is 20 which is 18 or more.