0
0
Rustprogramming~10 mins

Arithmetic operators in Rust - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Arithmetic operators
Start
Evaluate operands
Apply operator (+, -, *, /, %)
Calculate result
Use result in expression or output
End
This flow shows how Rust evaluates arithmetic expressions by first evaluating operands, then applying the operator to get the result.
Execution Sample
Rust
fn main() {
    let a = 10;
    let b = 3;
    let sum = a + b;
    println!("Sum: {}", sum);
}
This code adds two numbers and prints the sum.
Execution Table
StepActionOperandsOperatorResultOutput
1Assign a--10-
2Assign b--3-
3Calculate sum10 and 3+13-
4Print sum---Sum: 13
5End----
💡 Program ends after printing the sum.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
auninitialized10101010
buninitializeduninitialized333
sumuninitializeduninitializeduninitialized1313
Key Moments - 3 Insights
Why does the sum variable hold 13 after step 3?
Because at step 3, Rust adds the values of a (10) and b (3) using the + operator, resulting in 13 as shown in the execution_table row 3.
What happens if we try to divide by zero using / operator?
Rust will panic at runtime because division by zero is not allowed. This is not shown in the current table but is important to remember.
Why is there no output until step 4?
Because the println! macro is called only at step 4, which prints the result to the console.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'sum' after step 3?
A13
B10
C3
Dundefined
💡 Hint
Check the 'Result' column in row 3 of the execution_table.
At which step does the program print the output?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Output' column in the execution_table.
If we change operator from + to *, what will be the result at step 3?
A7
B30
C13
DError
💡 Hint
Multiply 10 and 3 to find the new result.
Concept Snapshot
Arithmetic operators in Rust: + (add), - (subtract), * (multiply), / (divide), % (modulus).
Operands are evaluated first, then operator is applied.
Result is stored or used immediately.
Division by zero causes runtime panic.
Use println! to output results.
Full Transcript
This visual execution traces arithmetic operators in Rust. First, variables a and b are assigned values 10 and 3. Then, the sum variable is calculated by adding a and b using the + operator, resulting in 13. Finally, the sum is printed to the console. The execution table shows each step with actions, operands, operators, results, and output. The variable tracker shows how a, b, and sum change over time. Key moments clarify why sum is 13, when output happens, and what happens with division by zero. The quiz tests understanding of variable values, output timing, and operator effects. The snapshot summarizes key points about arithmetic operators in Rust.