Operator precedence determines the order in which parts of an expression are evaluated. Operators with higher precedence are evaluated before those with lower precedence.
Click to reveal answer
beginner
Which operator has higher precedence: multiplication (*) or addition (+)?
Multiplication (*) has higher precedence than addition (+). So in an expression like 2 + 3 * 4, the multiplication happens first.
Click to reveal answer
beginner
How does parentheses affect operator precedence?
Parentheses change the normal precedence by forcing the expression inside them to be evaluated first, regardless of operator precedence.
Click to reveal answer
intermediate
In Rust, which has higher precedence: logical AND (&&) or logical OR (||)?
Logical AND (&&) has higher precedence than logical OR (||). So && operations are evaluated before || in expressions.
Click to reveal answer
intermediate
What happens if operators have the same precedence?
If operators have the same precedence, Rust evaluates them based on their associativity, usually left to right for most operators.
Click to reveal answer
In Rust, which operator is evaluated first in the expression 5 + 4 * 3?
AAddition (+)
BMultiplication (*)
CBoth at the same time
DDepends on variable types
✗ Incorrect
Multiplication (*) has higher precedence than addition (+), so 4 * 3 is evaluated first.
What does parentheses do in the expression (5 + 4) * 3?
AThey have no effect
BThey make addition happen after multiplication
CThey force addition to happen before multiplication
DThey change the variable types
✗ Incorrect
Parentheses force the expression inside to be evaluated first, so 5 + 4 happens before multiplication.
Which operator has higher precedence in Rust: && or ||?
A&& (logical AND)
BDepends on the expression
CThey have the same precedence
D|| (logical OR)
✗ Incorrect
Logical AND (&&) has higher precedence than logical OR (||).
If two operators have the same precedence, how does Rust decide which to evaluate first?
AIt always evaluates right to left
BRandomly
CBased on variable names
DBased on associativity (usually left to right)
✗ Incorrect
Rust uses associativity rules, usually left to right, to decide evaluation order for operators with the same precedence.
In the expression 10 - 3 - 2, how is the evaluation done?
A(10 - 3) - 2
B10 - (3 - 2)
CAll at once
DDepends on compiler
✗ Incorrect
Subtraction is left-associative, so it evaluates from left to right: (10 - 3) - 2.
Explain operator precedence and how it affects the evaluation of expressions in Rust.
Think about which parts of an expression are calculated first.
You got /4 concepts.
Describe how Rust handles operators with the same precedence and give an example.
Consider how expressions like 10 - 3 - 2 are evaluated.
You got /3 concepts.
Practice
(1/5)
1. In Rust, which operator has the highest precedence among these: +, *, -, /?
easy
A. Multiplication (*) and division (/) have higher precedence than addition (+) and subtraction (-).
B. Addition (+) and subtraction (-) have higher precedence than multiplication (*) and division (/).
C. All operators have the same precedence and are evaluated left to right.
D. Subtraction (-) has the highest precedence among all.
Solution
Step 1: Recall Rust operator precedence rules
Multiplication (*) and division (/) operators have higher precedence than addition (+) and subtraction (-).
Step 2: Compare given operators
Among the given operators, * and / are evaluated before + and - in expressions.
Final Answer:
Multiplication (*) and division (/) have higher precedence than addition (+) and subtraction (-). -> Option A
Quick Check:
*, / > +, - [OK]
Hint: Remember: multiply/divide before add/subtract [OK]
Common Mistakes:
Thinking addition has higher precedence than multiplication
Assuming all operators have equal precedence
Confusing subtraction precedence with multiplication
2. Which of the following Rust expressions is correctly written to add 2 and 3, then multiply the result by 4?
easy
A. 2 + 3 * 4
B. (2 + 3) * 4
C. 2 + (3 * 4)
D. 2 * 3 + 4
Solution
Step 1: Understand operator precedence and parentheses
Multiplication (*) has higher precedence than addition (+), so without parentheses, multiplication happens first.
Step 2: Check each expression
(2 + 3) * 4 uses parentheses to add 2 and 3 first, then multiplies by 4, which matches the requirement.
Final Answer:
(2 + 3) * 4 -> Option B
Quick Check:
Parentheses force addition first [OK]
Hint: Use parentheses to change default order [OK]
Common Mistakes:
Ignoring parentheses and relying on default precedence
Misplacing parentheses causing wrong order
Assuming left-to-right always applies
3. What is the output of this Rust code?
let result = 10 - 4 / 2 + 3 * 2;
println!("{}", result);
medium
A. 13
B. 10
C. 11
D. 14
Solution
Step 1: Apply operator precedence to expression
Division and multiplication happen before addition and subtraction: 4 / 2 = 2 3 * 2 = 6