Bird
Raised Fist0
Rustprogramming~10 mins

Assignment operators in Rust - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Assignment operators
Start
↓
Initialize variable
↓
Apply assignment operator
↓
Update variable value
↓
Use updated value
↓
End
This flow shows how a variable is initialized, then updated using an assignment operator, and finally used with its new value.
Execution Sample
Rust
let mut x = 5;
x += 3;
println!("{}", x);
This code initializes x with 5, adds 3 to x using the += operator, then prints the updated value.
Execution Table
StepCode executedVariable x valueExplanation
1let mut x = 5;5Variable x is created and set to 5
2x += 3;8Add 3 to x, so x becomes 8
3println!("{}", x);8Print the current value of x, which is 8
4End of program8Program finishes with x = 8
💡 Program ends after printing the updated value of x.
Variable Tracker
VariableStartAfter Step 2Final
x588
Key Moments - 2 Insights
Why do we need to declare x as mutable with 'mut'?
In Rust, variables are immutable by default. The execution_table row 1 shows 'let mut x = 5;' which means x can be changed. Without 'mut', the assignment operator 'x += 3;' in row 2 would cause a compilation error.
What does the '+=' operator do exactly?
The '+=' operator adds the right side value to the variable and updates it. Execution_table row 2 shows x changing from 5 to 8 by adding 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of x after step 2?
A5
B8
C3
D0
💡 Hint
Check the 'Variable x value' column at step 2 in the execution_table.
At which step does the program print the value of x?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look for the println! statement in the 'Code executed' column of the execution_table.
If we remove 'mut' from the variable declaration, what happens at step 2?
Ax becomes 8 without error
Bx becomes 3
CCompilation error because x is immutable
DProgram prints 5
💡 Hint
Refer to key_moments explanation about the need for 'mut' in variable declaration.
Concept Snapshot
Assignment operators in Rust update a variable's value.
Use 'mut' to make variables changeable.
Example: x += 3 adds 3 to x.
Other operators: -=, *=, /=, %=.
They combine operation and assignment in one step.
Full Transcript
This visual trace shows how assignment operators work in Rust. First, a variable x is created with value 5 and marked mutable using 'mut'. Then, the '+=' operator adds 3 to x, updating its value to 8. Finally, the program prints the updated value 8. The key point is that variables must be mutable to use assignment operators that change their value. The execution table tracks each step and the variable's value changes. The quiz questions help check understanding of these steps and the role of 'mut'.

Practice

(1/5)
1. What does the Rust assignment operator += do?
easy
A. Subtracts the right value from the left variable and stores the result in the variable
B. Adds the right value to the left variable and stores the result in the variable
C. Multiplies the right value with the left variable and stores the result in the variable
D. Divides the left variable by the right value and stores the result in the variable

Solution

  1. Step 1: Understand the += operator

    The += operator adds the right-hand side value to the left-hand side variable.
  2. Step 2: Effect on the variable

    The result of the addition is stored back into the same variable, updating its value.
  3. Final Answer:

    Adds the right value to the left variable and stores the result in the variable -> Option B
  4. Quick Check:

    += means add and assign [OK]
Hint: Think: add then store back in the same variable [OK]
Common Mistakes:
  • Confusing += with subtraction or multiplication
  • Forgetting that the variable must be mutable
  • Thinking it creates a new variable instead of updating
2. Which of the following is the correct way to use the *= operator in Rust?
easy
A. let x = 5; x *= 2;
B. let mut x = 5; *= x 2;
C. let x = 5; mut x *= 2;
D. let mut x = 5; x *= 2;

Solution

  1. Step 1: Check variable mutability

    In Rust, to change a variable's value, it must be declared with mut. So let mut x = 5; is correct.
  2. Step 2: Correct operator usage

    The operator *= is used as x *= 2; to multiply and assign.
  3. Final Answer:

    let mut x = 5; x *= 2; -> Option D
  4. Quick Check:

    Mutable variable + operator syntax = correct [OK]
Hint: Declare variable mutable before using assignment operators [OK]
Common Mistakes:
  • Not declaring variable as mutable
  • Placing mut in wrong position
  • Incorrect operator syntax like *= x 2
3. What is the output of the following Rust code?
let mut a = 10;
a += 5;
a *= 2;
println!("{}", a);
medium
A. 30
B. 25
C. 20
D. 15

Solution

  1. Step 1: Perform a += 5;

    Starting with a = 10, adding 5 gives a = 15.
  2. Step 2: Perform a *= 2;

    Multiplying 15 by 2 updates a = 30.
  3. Final Answer:

    30 -> Option A
  4. Quick Check:

    10 + 5 = 15; 15 * 2 = 30 [OK]
Hint: Calculate step-by-step updates to variable [OK]
Common Mistakes:
  • Adding and multiplying in wrong order
  • Forgetting variable is mutable
  • Misreading operator effects
4. Identify the error in this Rust code snippet:
let x = 8;
x -= 3;
println!("{}", x);
medium
A. The println! macro is used incorrectly
B. The operator -= is not valid in Rust
C. Variable x is not mutable but is modified
D. Missing semicolon after println!

Solution

  1. Step 1: Check variable mutability

    The variable x is declared without mut, so it is immutable.
  2. Step 2: Check assignment operator usage

    The operator -= tries to subtract and assign a new value to x, which is not allowed for immutable variables.
  3. Final Answer:

    Variable x is not mutable but is modified -> Option C
  4. Quick Check:

    Immutable variable cannot be changed [OK]
Hint: Mutable variables needed for assignment operators [OK]
Common Mistakes:
  • Assuming all variables are mutable by default
  • Thinking operator syntax is wrong
  • Ignoring Rust's immutability rules
5. Given the code below, what is the final value of result?
let mut result = 50;
let change = 7;
result %= change;
result += 3;
result *= 4;
hard
A. 16
B. 12
C. 28
D. 14

Solution

  1. Step 1: Calculate result %= change;

    50 % 7 equals 1 because 7 * 7 = 49 and remainder is 1, so result = 1.
  2. Step 2: Calculate result += 3;

    Add 3 to 1 gives result = 4.
  3. Step 3: Calculate result *= 4;

    Multiply 4 by 4 gives result = 16.
  4. Final Answer:

    16 -> Option A
  5. Quick Check:

    50 % 7 = 1; 1 + 3 = 4; 4 * 4 = 16 [OK]
Hint: Apply each operator stepwise carefully [OK]
Common Mistakes:
  • Miscomputing modulus operation
  • Skipping steps or doing operations out of order
  • Choosing wrong multiplication 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