An if–else expression in Rust lets you choose between two paths based on a condition. It evaluates to a value, so you can assign its result to a variable.
Click to reveal answer
beginner
How do you write a simple if–else expression in Rust?
You write it like this:<br><pre>let x = if condition { value1 } else { value2 };</pre><br>The variable x gets value1 if the condition is true, otherwise value2.
Click to reveal answer
intermediate
Can if–else expressions in Rust return different types?
No. Both branches of an if–else expression must return the same type because Rust needs to know the exact type of the expression's result.
Click to reveal answer
intermediate
What happens if you omit the else branch in an if expression?
If you omit else, the if expression returns () (unit type) when the condition is false. This can cause type mismatch if you try to assign it to a variable expecting another type.
Click to reveal answer
beginner
Why is if–else called an expression in Rust, not just a statement?
Because it produces a value that can be used in assignments or other expressions. This makes Rust code concise and expressive.
Click to reveal answer
What does this Rust code do?
let x = if 5 > 3 { 10 } else { 20 };
AAssigns 5 to x
BAssigns 20 to x because 5 > 3 is false
CCauses a compile error
DAssigns 10 to x because 5 > 3 is true
✗ Incorrect
Since 5 is greater than 3, the condition is true, so x gets 10.
In Rust, what type must both branches of an if–else expression return?
AThey can return any types
BThey must return the same type
COne branch returns a type, the other returns unit
DThey must return integers
✗ Incorrect
Both branches must return the same type so Rust knows the expression's type.
What is the value of this expression if the condition is false?
if false { 1 } else { 2 }
A2
B1
C()
DCompile error
✗ Incorrect
The else branch runs when the condition is false, so the value is 2.
What happens if you write:
let x = if true { 5 };
?
ACompile error because else is missing
Bx is ()
Cx is uninitialized
Dx is 5
✗ Incorrect
Rust requires else branch for if used as an expression to ensure both branches return a value.
Why is if–else useful as an expression in Rust?
ABecause it is faster than other languages
BBecause it can only run statements
CBecause it returns a value that can be assigned
DBecause it does not need braces
✗ Incorrect
If–else expressions return values, allowing concise and clear code.
Explain how an if–else expression works in Rust and why both branches must return the same type.
Think about how Rust needs to know the type of the whole expression.
You got /4 concepts.
Describe what happens if you use an if expression without an else branch when assigning to a variable.
Consider what Rust returns when the condition is false and no else is given.
You got /4 concepts.
Practice
(1/5)
1. What does an if-else expression in Rust do?
easy
A. Declares a variable without value
B. Repeats a block of code multiple times
C. Defines a new function
D. Chooses between two options and returns a value
Solution
Step 1: Understand the purpose of if-else
An if-else expression lets the program pick one of two paths based on a condition.
Step 2: Recognize that it returns a value
In Rust, if-else expressions return a value that can be stored in a variable.
Final Answer:
Chooses between two options and returns a value -> Option D
Quick Check:
If-else expression = choice with value [OK]
Hint: If-else picks one path and returns a value [OK]
Common Mistakes:
Thinking if-else repeats code like a loop
Confusing if-else with function definition
Ignoring that if-else returns a value
2. Which of the following is the correct syntax for an if-else expression in Rust?
easy
A. let x = if condition { 5 } else { 10 };
B. if (condition) then { 5 } else { 10 };
C. let x = if condition: 5 else: 10;
D. let x = if condition { 5 } elseif { 10 };
Solution
Step 1: Check Rust if-else syntax
Rust uses if condition { value1 } else { value2 } with braces and no parentheses.
Step 2: Identify correct option
let x = if condition { 5 } else { 10 }; matches Rust syntax exactly with variable assignment and braces.
Final Answer:
let x = if condition { 5 } else { 10 }; -> Option A
Quick Check:
Rust if-else syntax = let x = if condition { 5 } else { 10 }; [OK]
Hint: Use braces and no parentheses in Rust if-else [OK]
Common Mistakes:
Using parentheses around condition
Writing elseif instead of else if
Using colons instead of braces
3. What is the output of this Rust code?
let number = 7;
let result = if number % 2 == 0 { "even" } else { "odd" };
println!("{}", result);
medium
A. odd
B. even
C. 7
D. Syntax error
Solution
Step 1: Check the condition number % 2 == 0
7 % 2 equals 1, which is not 0, so condition is false.
Step 2: Determine which branch runs
Since condition is false, the else branch runs, returning "odd".
Final Answer:
odd -> Option A
Quick Check:
7 is odd, so output = odd [OK]
Hint: Check condition result to pick if or else branch [OK]
Common Mistakes:
Assuming 7 is even
Confusing modulo operator
Expecting number to print instead of result
4. Identify the error in this Rust code:
let x = 10;
let y = if x > 5 { 1 };
println!("{}", y);
medium
A. Variable y is not declared
B. Incorrect comparison operator
C. Missing else branch in if expression
D. println! macro syntax error
Solution
Step 1: Check if-else expression completeness
When assigning with if, Rust requires both if and else branches to return a value.
Step 2: Identify missing else branch
The code has no else branch, so y's value is undefined, causing a compile error.
Final Answer:
Missing else branch in if expression -> Option C
Quick Check:
if-else must have else when used as expression [OK]
Hint: Always include else when assigning from if [OK]
Common Mistakes:
Forgetting else branch when assigning
Thinking if alone returns a value
Misreading error as println! issue
5. You want to assign a variable grade based on a score: if score is 90 or above, grade is 'A', else 'F'. Which code correctly uses if-else expression?
hard
A. let grade = if score > 90 { 'A' } else if score < 90 { 'F' };
B. let grade = if score >= 90 { 'A' } else { 'F' };
C. let grade = if score >= 90 { 'A' }; else { 'F' };
D. let grade = if score >= 90 then { 'A' } else { 'F' };
Solution
Step 1: Check correct if-else expression syntax
Rust uses if condition { value } else { value } without 'then' or semicolons inside.
Step 2: Validate condition and branches
let grade = if score >= 90 { 'A' } else { 'F' }; correctly checks >= 90 and returns 'A' or 'F' in else branch.
Final Answer:
let grade = if score >= 90 { 'A' } else { 'F' }; -> Option B
Quick Check:
Correct syntax and logic = let grade = if score >= 90 { 'A' } else { 'F' }; [OK]
Hint: Use braces and else for complete if-else expression [OK]
Common Mistakes:
Using 'then' keyword
Missing else branch
Incorrect semicolon placement
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.