0
0
Rustprogramming~10 mins

Operator precedence in Rust - Step-by-Step Execution

Choose your learning style9 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.