Recall & Review
beginner
What is an
if expression in Rust?An
if expression in Rust lets you run code only when a condition is true. It can also return a value, making it more than just a decision maker.Click to reveal answer
beginner
How do you write a simple
if expression in Rust?Use
if condition { /* code */ }. For example: <br>if x > 5 { println!("x is big"); }Click to reveal answer
intermediate
Can
if expressions return values in Rust?Yes! You can assign the result of an <code>if</code> expression to a variable. For example: <br><code>let number = if condition { 5 } else { 10 };</code>Click to reveal answer
intermediate
What happens if you omit the
else branch in an if expression used for assignment?Rust will give an error because the
if expression must return a value in all cases when assigned to a variable.Click to reveal answer
beginner
How do you write multiple conditions with
if in Rust?Use
else if for additional conditions. For example: <br>if x > 5 { ... } else if x == 5 { ... } else { ... }Click to reveal answer
What does an
if expression in Rust return when used in an assignment?✗ Incorrect
An
if expression returns the value from the branch that runs, which can be assigned to a variable.Which of these is a correct way to write an
if expression in Rust?✗ Incorrect
Rust uses
if condition { } syntax without 'then' or colons.What error occurs if you assign an
if expression without an else branch?✗ Incorrect
Rust expects all branches to return the same type; missing
else means no value for some cases, causing a type mismatch.How do you check multiple conditions in Rust?
✗ Incorrect
Rust uses
else if to chain multiple conditions.What type must the values returned by all branches of an
if expression have?✗ Incorrect
All branches must return the same type so Rust knows what type the
if expression returns.Explain how an
if expression works in Rust and how it can be used to assign values.Think about how you decide between two options and store the result.
You got /5 concepts.
Describe what happens if you use an
if expression without an else branch when assigning to a variable.Consider what Rust expects when it needs a value from all paths.
You got /4 concepts.