An expression in Rust is any piece of code that returns a value. For example, 5 + 3 is an expression that evaluates to 8.
Click to reveal answer
beginner
How does Rust evaluate the expression 2 * (3 + 4)?
Rust first evaluates the expression inside the parentheses (3 + 4), which is 7, then multiplies it by 2, resulting in 14.
Click to reveal answer
intermediate
What is the difference between an expression and a statement in Rust?
An expression returns a value and can be part of a larger expression. A statement performs an action but does not return a value. For example, <code>let x = 5;</code> is a statement, while <code>5 + 3</code> is an expression.
Click to reveal answer
beginner
How does Rust handle boolean expressions like 5 > 3 && 2 < 4?
Rust evaluates each comparison: 5 > 3 is true, 2 < 4 is true, then combines them with && (and), resulting in true.
Click to reveal answer
intermediate
What is the role of the match expression in Rust?
The match expression lets you compare a value against patterns and execute code based on which pattern matches. It always returns a value, making it a powerful expression for decision-making.
Click to reveal answer
What does the Rust expression 3 + 4 * 2 evaluate to?
A11
B14
C10
D7
✗ Incorrect
Rust follows order of operations: multiplication first (4 * 2 = 8), then addition (3 + 8 = 11).
Which of the following is a statement in Rust?
A<code>let x = 10;</code>
B<code>3 * 4</code>
C<code>5 + 3</code>
D<code>7 - 2</code>
✗ Incorrect
let x = 10; is a statement because it declares a variable and does not return a value.
What is the result of true || false && false in Rust?
Afalse
Btrue
Cerror
Dnone
✗ Incorrect
Rust evaluates && before ||. So false && false is false, then true || false is true.
Which expression correctly uses match to return a value?
Amatch num { 1 => 10; _ => 20; };
Bmatch num { 1 => println!("One") };
Clet x = match num { 1 => 10, _ => 20 };
Dlet x = if num == 1 { 10 } else { 20 };
✗ Incorrect
Option C uses match as an expression that returns a value assigned to x.
What type of value does an expression in Rust always produce?
AOnly integers
BNo value
COnly booleans
DA value or unit type <code>()</code>
✗ Incorrect
Every expression in Rust produces a value. If no meaningful value, it produces the unit type ().
Explain how Rust evaluates the expression 4 + 3 * 2 step-by-step.
Think about which part Rust calculates first.
You got /3 concepts.
Describe the difference between an expression and a statement in Rust with examples.
Consider what each piece of code produces or does.
You got /4 concepts.
Practice
(1/5)
1. What does the following Rust expression do? let result = 5 + 3 * 2; Choose the correct description.
easy
A. Calculates 3 times 2, then adds 5
B. Multiplies 5 by 3, then adds 2
C. Calculates 5 plus 3, then multiplies by 2
D. Adds 5, 3, and 2 together
Solution
Step 1: Understand operator precedence in Rust
Multiplication (*) has higher precedence than addition (+), so 3 * 2 is calculated first.
Step 2: Calculate the expression step-by-step
3 * 2 = 6, then 5 + 6 = 11.
Final Answer:
Calculates 3 times 2, then adds 5 -> Option A
Quick Check:
5 + (3 * 2) = 11 [OK]
Hint: Remember multiplication before addition in expressions [OK]
Common Mistakes:
Adding numbers left to right ignoring precedence
Multiplying after addition
Confusing operator precedence
2. Which of the following is the correct way to assign the result of 4 + 7 to a variable named sum in Rust?
easy
A. let sum : 4 + 7;
B. sum = let 4 + 7;
C. let sum = 4 + 7;
D. let sum == 4 + 7;
Solution
Step 1: Recall Rust variable assignment syntax
In Rust, use let variable = expression; to assign a value.
Step 2: Check each option for correct syntax
let sum = 4 + 7; uses correct syntax: let sum = 4 + 7;. Others have syntax errors.
Final Answer:
let sum = 4 + 7; -> Option C
Quick Check:
Correct Rust assignment syntax [OK]
Hint: Use 'let var = expression;' to assign values [OK]
Common Mistakes:
Using '==' instead of '=' for assignment
Placing 'let' after variable name
Using ':' instead of '=' for assignment
3. What is the output of this Rust code?
fn main() {
let x = 10;
let y = 3;
let result = x / y + x % y;
println!("{}", result);
}