Bird
Raised Fist0
Rustprogramming~10 mins

Arithmetic operators in Rust - Step-by-Step Execution

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

Practice

(1/5)
1. Which arithmetic operator in Rust is used to find the remainder of a division?
easy
A. The - operator
B. The / operator
C. The * operator
D. The % operator

Solution

  1. Step 1: Understand the meaning of each operator

    The / operator divides, * multiplies, - subtracts, and % gives the remainder after division.
  2. Step 2: Identify the operator for remainder

    The % operator returns the remainder when one number is divided by another.
  3. Final Answer:

    The % operator -> Option D
  4. Quick Check:

    Remainder operator = % [OK]
Hint: Remainder always uses % in math [OK]
Common Mistakes:
  • Confusing / with %, thinking / gives remainder
  • Using * or - for remainder
  • Mixing up operator symbols
2. Which of the following is the correct way to write multiplication of two variables a and b in Rust?
easy
A. a * b
B. a x b
C. a . b
D. a & b

Solution

  1. Step 1: Recall Rust arithmetic syntax

    Rust uses * for multiplication, not x, ., or &.
  2. Step 2: Confirm correct operator

    The expression a * b correctly multiplies a and b.
  3. Final Answer:

    a * b -> Option A
  4. Quick Check:

    Multiplication operator = * [OK]
Hint: Use * for multiplication in Rust [OK]
Common Mistakes:
  • Using x instead of *
  • Using . or & which are not arithmetic
  • Confusing syntax with other languages
3. What is the output of the following Rust code?
let x = 10;
let y = 3;
let result = x / y;
println!("{}", result);
medium
A. 4
B. 3.3333
C. 3
D. Syntax error

Solution

  1. Step 1: Understand integer division in Rust

    Both x and y are integers, so x / y performs integer division, dropping decimals.
  2. Step 2: Calculate integer division

    10 divided by 3 is 3.3333, but integer division truncates to 3.
  3. Final Answer:

    3 -> Option C
  4. Quick Check:

    Integer division truncates decimals = 3 [OK]
Hint: Integer division drops decimals, use floats for precise result [OK]
Common Mistakes:
  • Expecting decimal result with integer division
  • Confusing integer and float division
  • Assuming syntax error due to println!
4. Identify the error in this Rust code snippet:
let a = 5;
let b = 2;
let c = a / b.0;
println!("{}", c);
medium
A. Variables a and b are not declared
B. Using .0 on integer causes error
C. Missing semicolon after println!
D. Division operator / is invalid

Solution

  1. Step 1: Analyze the expression a / b.0

    The code tries to access .0 on b, which is an integer, not a tuple or struct.
  2. Step 2: Understand why .0 causes error

    Only tuples or structs have fields like .0; integers do not, so this causes a compile error.
  3. Final Answer:

    Using .0 on integer causes error -> Option B
  4. Quick Check:

    Integer has no .0 field = error [OK]
Hint: Only tuples/structs have .0, not integers [OK]
Common Mistakes:
  • Thinking / is invalid operator
  • Missing semicolon causes error
  • Assuming variables are undeclared
5. You want to calculate the average of two integers a and b in Rust and get a float result. Which code snippet correctly does this?
hard
A. let avg = (a + b) as f64 / 2.0;
B. let avg = (a + b) / 2.0;
C. let avg = (a + b) / 2;
D. let avg = (a + b) / 2 as f64;

Solution

  1. Step 1: Understand type conversion for float division

    To get a float result, convert integers to float before division.
  2. Step 2: Check each option for correct syntax and types

    let avg = (a + b) / 2; does integer division, no float result. let avg = (a + b) as f64 / 2.0; converts sum to f64, divides by 2.0 (float), correct. let avg = (a + b) / 2.0; divides integer sum by float 2.0 without conversion, invalid. let avg = (a + b) / 2 as f64; tries to cast 2 to f64 before division (due to `as` precedence), invalid.
  3. Final Answer:

    let avg = (a + b) as f64 / 2.0; -> Option A
  4. Quick Check:

    Convert to float before dividing by float [OK]
Hint: Cast sum to float before dividing by float [OK]
Common Mistakes:
  • Dividing integers by float without cast
  • Casting after division
  • Using integer division expecting float result
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