Bird
Raised Fist0
Rustprogramming~10 mins

Operator precedence in Rust - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Operator precedence
Start Expression
↓
Identify Operators
↓
Check Precedence
↓
Evaluate Higher Precedence
↓
Replace Result
↓
Evaluate Next Operator
↓
Final Result
The program reads an expression, identifies operators, evaluates them in order of precedence from highest to lowest, and combines results step-by-step until the final value is computed.
Execution Sample
Rust
let result = 2 + 3 * 4 - 5 / 5;
println!("{}", result);
Calculates the expression 2 + 3 * 4 - 5 / 5 following Rust's operator precedence rules.
Execution Table
StepExpressionOperationPrecedenceResultExplanation
12 + 3 * 4 - 5 / 53 * 4Multiplication (High)12Multiply 3 and 4 first
22 + 12 - 5 / 55 / 5Division (High)1Divide 5 by 5 next
32 + 12 - 12 + 12Addition (Lower)14Add 2 and 12
414 - 114 - 1Subtraction (Lower)13Subtract 1 from 14
513NoneNone13Final result after all operations
💡 All operators evaluated in precedence order; final result is 13
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
resultundefined2 + 12 - 5 / 52 + 12 - 114 - 11313
Key Moments - 3 Insights
Why is multiplication done before addition in the expression?
Because multiplication has higher precedence than addition, as shown in execution_table steps 1 and 3 where multiplication is evaluated first.
Why does division happen before subtraction?
Division has higher precedence than subtraction, so it is evaluated earlier as seen in execution_table step 2 before subtraction in step 4.
What happens if we ignore operator precedence and evaluate left to right?
The result would be different and incorrect; operator precedence ensures correct order, as shown by the stepwise evaluation in the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result after step 2?
A2 + 12 - 1
B2 + 3 * 4 - 1
C14 - 1
D13
💡 Hint
Check the 'Expression' column in execution_table row for step 2
At which step is the addition operation evaluated?
AStep 1
BStep 3
CStep 4
DStep 2
💡 Hint
Look at the 'Operation' column in execution_table to find when '2 + 12' is calculated
If the division operator had lower precedence than subtraction, which step would change?
AStep 4
BStep 3
CStep 2
DStep 1
💡 Hint
Consider when division is evaluated in execution_table and how precedence affects order
Concept Snapshot
Operator precedence determines the order in which parts of an expression are calculated.
Higher precedence operators like * and / are done before + and -.
Rust follows standard math precedence rules.
Evaluate operators from highest to lowest precedence step-by-step.
Parentheses can override precedence to group operations.
Full Transcript
This example shows how Rust evaluates the expression 2 + 3 * 4 - 5 / 5 by following operator precedence rules. Multiplication and division have higher precedence than addition and subtraction, so 3 * 4 and 5 / 5 are calculated first, resulting in 12 and 1 respectively. Then addition and subtraction are performed left to right: 2 + 12 equals 14, and 14 - 1 equals 13. The final result printed is 13. This stepwise evaluation ensures correct calculation order and result.

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

  1. Step 1: Recall Rust operator precedence rules

    Multiplication (*) and division (/) operators have higher precedence than addition (+) and subtraction (-).
  2. Step 2: Compare given operators

    Among the given operators, * and / are evaluated before + and - in expressions.
  3. Final Answer:

    Multiplication (*) and division (/) have higher precedence than addition (+) and subtraction (-). -> Option A
  4. 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

  1. Step 1: Understand operator precedence and parentheses

    Multiplication (*) has higher precedence than addition (+), so without parentheses, multiplication happens first.
  2. Step 2: Check each expression

    (2 + 3) * 4 uses parentheses to add 2 and 3 first, then multiplies by 4, which matches the requirement.
  3. Final Answer:

    (2 + 3) * 4 -> Option B
  4. 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

  1. Step 1: Apply operator precedence to expression

    Division and multiplication happen before addition and subtraction:
    4 / 2 = 2
    3 * 2 = 6
  2. Step 2: Calculate left to right with results

    Expression becomes: 10 - 2 + 6
    10 - 2 = 8
    8 + 6 = 14
  3. Final Answer:

    14 -> Option D
  4. Quick Check:

    */ before +- gives 14 [OK]
Hint: Calculate * and / first, then + and - left to right [OK]
Common Mistakes:
  • Doing operations strictly left to right ignoring precedence
  • Adding before multiplying
  • Miscomputing division or multiplication
4. Identify the error in this Rust expression:
let value = 5 + 3 * / 2;
medium
A. No error, expression is valid
B. Logical error: multiplication should be addition
C. Syntax error due to misplaced operator '/'
D. Runtime error due to division by zero

Solution

  1. Step 1: Analyze the expression syntax

    The expression has * / operators together without a number between them, which is invalid syntax in Rust.
  2. Step 2: Confirm error type

    This causes a syntax error because Rust expects an operand between operators.
  3. Final Answer:

    Syntax error due to misplaced operator '/' -> Option C
  4. Quick Check:

    Operators must have operands between them [OK]
Hint: Check for missing operands between operators [OK]
Common Mistakes:
  • Assuming expression is valid without checking syntax
  • Confusing syntax error with runtime error
  • Ignoring operator placement rules
5. Given the Rust expression:
let x = 2 + 3 * 4 - 6 / 3 + (8 - 5) * 2;

What is the correct value of x after evaluation?
hard
A. 18
B. 20
C. 19
D. 21

Solution

  1. Step 1: Evaluate parentheses and high precedence operators

    Calculate inside parentheses:
    (8 - 5) = 3
    Then multiplication and division:
    3 * 4 = 12
    6 / 3 = 2
    3 * 2 = 6 (from parentheses result * 2)
  2. Step 2: Substitute and calculate addition and subtraction left to right

    Expression becomes: 2 + 12 - 2 + 6
    2 + 12 = 14
    14 - 2 = 12
    12 + 6 = 18
  3. Final Answer:

    18 -> Option A
  4. Quick Check:

    Parentheses and */ first, then +- left to right [OK]
Hint: Calculate parentheses first, then * and /, then + and - left to right [OK]
Common Mistakes:
  • Ignoring parentheses and calculating in wrong order
  • Doing addition before multiplication
  • Mixing up left-to-right evaluation for + and -
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard