0
0
Rustprogramming~15 mins

Nested conditions in Rust - Mini Project: Build & Apply

Choose your learning style9 modes available
Nested conditions
๐Ÿ“– Scenario: You are creating a simple program that checks a person's age and whether they have a membership card to decide if they can enter a club.
๐ŸŽฏ Goal: Build a Rust program that uses nested if conditions to check if a person is old enough and has a membership card to enter the club.
๐Ÿ“‹ What You'll Learn
Create a variable age with the exact value 20.
Create a variable has_membership with the exact value true.
Use nested if conditions to check if age is 18 or more and if has_membership is true.
Print "Access granted" if both conditions are true, otherwise print "Access denied".
๐Ÿ’ก Why This Matters
๐ŸŒ Real World
Many places like clubs or websites check multiple rules before allowing access. Nested conditions help check these rules clearly.
๐Ÿ’ผ Career
Understanding nested conditions is important for writing programs that make decisions based on several factors, a common task in software development.
Progress0 / 4 steps
1
Create the initial variables
Create a variable called age and set it to 20. Also create a variable called has_membership and set it to true.
Rust
Need a hint?

Use let to create variables. For example, let age = 20;.

2
Add the first condition to check age
Inside the main function, add an if condition to check if age is greater than or equal to 18.
Rust
Need a hint?

Use if age >= 18 { } to check the age.

3
Add nested condition to check membership
Inside the if age >= 18 block, add another if condition to check if has_membership is true.
Rust
Need a hint?

Use if has_membership { } inside the first if block.

4
Print access result based on conditions
Inside the nested if has_membership block, print "Access granted". Otherwise, print "Access denied" if either condition fails.
Rust
Need a hint?

Use println!("Access granted"); and println!("Access denied"); in the right places.