๐ Scenario: You are building a simple program to compare ages of two friends to see who is older.
๐ฏ Goal: Learn how to use relational operators in Rust to compare numbers and print the results.
๐ What You'll Learn
Create variables with exact values
Use relational operators: ==, !=, <, <=, >, >=
Print boolean results of comparisons
๐ก Why This Matters
๐ Real World
Comparing values is common in programs that make decisions, like checking user age for access or comparing scores in games.
๐ผ Career
Understanding relational operators is essential for any programming job because they help control the flow of programs and make decisions.
Progress0 / 4 steps
1
Create age variables
Create two variables called alice_age and bob_age with values 25 and 30 respectively.
Rust
Hint
Use let to create variables and assign the exact numbers.
2
Add relational comparisons
Add variables to store the results of these comparisons: alice_age == bob_age in is_equal, alice_age < bob_age in is_alice_younger, and alice_age >= bob_age in is_alice_older_or_equal.
Rust
Hint
Use ==, <, and >= operators to compare the ages and store the results in variables.
3
Add more relational comparisons
Add two more variables: is_not_equal for alice_age != bob_age and is_bob_younger_or_equal for bob_age <= alice_age.
Rust
Hint
Use != and <= operators for these comparisons.
4
Print all comparison results
Print the values of is_equal, is_alice_younger, is_alice_older_or_equal, is_not_equal, and is_bob_younger_or_equal each on a new line using println!.
Rust
Hint
Use println!("{}", variable); to print each boolean value on its own line.
Practice
(1/5)
1. Which of the following relational operators in Rust checks if two values are not equal?
easy
A. !=
B. ==
C. <=
D. >=
Solution
Step 1: Understand the meaning of relational operators
Relational operators compare two values and return true or false based on the comparison.
Step 2: Identify the operator for 'not equal'
The operator '!=' means 'not equal to' in Rust.
Final Answer:
!= -> Option A
Quick Check:
Not equal operator = != [OK]
Hint: Remember '!=' means not equal in most languages [OK]
Common Mistakes:
Confusing '!=' with '=='
Using '<>' which is not valid in Rust
Mixing up '<=' and '>='
2. Which of the following is the correct syntax to check if variable a is greater than or equal to variable b in Rust?
easy
A. a =>= b
B. a => b
C. a =< b
D. a >= b
Solution
Step 1: Recall Rust relational operator syntax
Rust uses '>=' to check if left value is greater than or equal to right value.
Step 2: Identify correct syntax among options
Only 'a >= b' matches the correct Rust syntax.
Final Answer:
a >= b -> Option D
Quick Check:
Greater or equal operator = >= [OK]
Hint: Greater or equal is '>=' not '=>' [OK]
Common Mistakes:
Using '=>' instead of '>='
Swapping '=' and '<' in operators
Confusing assignment '=' with comparison
3. What will be the output of the following Rust code?
let x = 10;
let y = 20;
println!("{}", x < y);
medium
A. true
B. 20
C. 10
D. false
Solution
Step 1: Understand the comparison operation
The code compares if x (10) is less than y (20), which is true.
Step 2: Determine the printed output
Since the condition is true, the printed output will be 'true'.
Final Answer:
true -> Option A
Quick Check:
10 < 20 = true [OK]
Hint: Compare values carefully; 10 is less than 20 [OK]
Common Mistakes:
Confusing output with variable values
Thinking it prints the smaller number
Mixing up < and > operators
4. Identify the error in the following Rust code snippet:
let a = 5;
let b = 10;
if a <> b {
println!("Not equal");
}
medium
A. Variables a and b must be mutable
B. The operator '<>' is invalid in Rust
C. Missing semicolon after println!
D. The if statement needs parentheses
Solution
Step 1: Check the relational operator used
The operator '<>' is not a valid relational operator in Rust.
Step 2: Confirm correct operator for not equal
Rust uses '!=' to check if two values are not equal.
Final Answer:
The operator '<>' is invalid in Rust -> Option B
Quick Check:
Not equal operator = !=, not <> [OK]
Hint: Use '!=' for not equal, not '<>' [OK]
Common Mistakes:
Using '<>' from other languages
Forgetting Rust requires '!='
Assuming parentheses needed in if condition
5. Given a list of integers, which Rust expression correctly checks if all elements are greater than 0 using relational operators?
hard
A. numbers.iter().filter(|&x| x > 0)
B. numbers.iter().any(|&x| x > 0)
C. numbers.iter().all(|&x| x > 0)
D. numbers.iter().map(|&x| x > 0)
Solution
Step 1: Understand the requirement
We want to check if all elements in the list are greater than 0.
Step 2: Identify the correct iterator method
'all' returns true only if every element satisfies the condition x > 0.
Step 3: Confirm the closure syntax
The closure '|&x| x > 0' correctly compares each element to 0.
Final Answer:
numbers.iter().all(|&x| x > 0) -> Option C
Quick Check:
Check all elements > 0 with 'all' method [OK]
Hint: Use 'all' to check every element meets condition [OK]
Common Mistakes:
Using 'any' which checks only one element
Using 'filter' which returns iterator, not bool
Using 'map' which transforms elements, not checks
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.