0
0
Rustprogramming~10 mins

Expression evaluation in Rust - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Expression evaluation
Start
Parse expression
Evaluate operands
Apply operator
Get result
End
The program reads an expression, evaluates each part, applies the operator, and produces the final result.
Execution Sample
Rust
fn main() {
    let result = 3 + 4 * 2;
    println!("{}", result);
}
This code calculates 3 plus 4 times 2 and prints the result.
Execution Table
StepActionEvaluationResult
1Evaluate 4 * 24 * 28
2Evaluate 3 + 83 + 811
3Print resultprintln!("{}", result)11
💡 Expression fully evaluated and result printed.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
resultuninitializeduninitialized1111
Key Moments - 2 Insights
Why is 4 * 2 evaluated before adding 3?
Because multiplication has higher priority than addition, so step 1 evaluates 4 * 2 first as shown in execution_table row 1.
Why does the variable 'result' change only after step 2?
The variable 'result' gets its value after the full expression is evaluated, which happens at step 2 in the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of the expression after step 1?
A11
B6
C8
D7
💡 Hint
Check the 'Result' column in execution_table row 1.
At which step is the variable 'result' assigned a value?
AStep 1
BStep 2
CStep 3
DBefore Step 1
💡 Hint
Look at variable_tracker row for 'result' and see when it changes from uninitialized.
If the expression was changed to (3 + 4) * 2, what would be the result after step 2?
A14
B10
C11
D7
💡 Hint
Parentheses change evaluation order; check how execution_table would change.
Concept Snapshot
Expression evaluation in Rust:
- Operators follow precedence (e.g., * before +)
- Evaluate operands first
- Apply operator to operands
- Store result in variable
- Print or use result
Example: let result = 3 + 4 * 2; // result is 11
Full Transcript
This example shows how Rust evaluates an expression step-by-step. First, it calculates 4 times 2, getting 8. Then it adds 3 to 8, resulting in 11. The variable 'result' is assigned this value. Finally, the program prints 11. Multiplication happens before addition because of operator precedence. Changing the expression with parentheses changes the order of evaluation and the final result.