The basic assignment operator in Rust is =. It assigns the value on the right to the variable on the left.
Click to reveal answer
beginner
What does the operator += do in Rust?
The += operator adds the value on the right to the variable on the left and then assigns the result back to the variable.
Click to reveal answer
beginner
How would you use the *= operator in Rust? Give an example.
The <code>*=</code> operator multiplies the variable by the value on the right and assigns the result back. Example: <code>let mut x = 5; x *= 3; // x is now 15</code>
Click to reveal answer
intermediate
Can you chain assignment operators in Rust like x += y += z? Why or why not?
No, Rust does not support chaining assignment operators like x += y += z. Each assignment must be a separate statement because assignment operators return () (unit), not a value.
Click to reveal answer
beginner
List some common compound assignment operators in Rust.
Common compound assignment operators in Rust include: +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=.
Click to reveal answer
What does the operator -= do in Rust?
ADivides the left variable by the right value
BAssigns the right value to the left variable without change
CMultiplies the left variable by the right value
DSubtracts the right value from the left variable and assigns the result to the variable
✗ Incorrect
The -= operator subtracts the right value from the left variable and stores the result back in the variable.
Which of these is NOT a valid compound assignment operator in Rust?
A*=
B+=
C++=
D&=
✗ Incorrect
++= is not a valid operator in Rust. Rust does not have increment (++) operators.
What is the result of this code? let mut a = 10; a /= 2;
Aa is 5
Ba is 20
Ca is 2
DCompilation error
✗ Incorrect
The /= operator divides a by 2 and assigns the result back, so a becomes 5.
Why can't you chain assignment operators like x += y += z in Rust?
ABecause Rust does not allow multiple assignments
BBecause assignment operators return unit type, not a value
CBecause Rust only supports <code>=</code> operator
DBecause variables must be immutable
✗ Incorrect
Assignment operators in Rust return the unit type (), so chaining like x += y += z is not allowed.
Which operator would you use to perform a bitwise AND and assign the result to a variable?
A&=
B|=
C^=
D&&=
✗ Incorrect
The &= operator performs a bitwise AND on the variable and the right value, then assigns the result back.
Explain how compound assignment operators work in Rust and give examples.
Think about how you can combine an operation and assignment in one step.
You got /4 concepts.
Why does Rust not allow chaining of assignment operators like x += y += z? What does this tell you about the return type of assignment operators?
Consider what value an assignment expression produces in Rust.
You got /4 concepts.
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
Step 1: Understand the += operator
The += operator adds the right-hand side value to the left-hand side variable.
Step 2: Effect on the variable
The result of the addition is stored back into the same variable, updating its value.
Final Answer:
Adds the right value to the left variable and stores the result in the variable -> Option B
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
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.
Step 2: Correct operator usage
The operator *= is used as x *= 2; to multiply and assign.
Final Answer:
let mut x = 5; x *= 2; -> Option D
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
Step 1: Perform a += 5;
Starting with a = 10, adding 5 gives a = 15.
Step 2: Perform a *= 2;
Multiplying 15 by 2 updates a = 30.
Final Answer:
30 -> Option A
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
Step 1: Check variable mutability
The variable x is declared without mut, so it is immutable.
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.
Final Answer:
Variable x is not mutable but is modified -> Option C
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
Step 1: Calculate result %= change;
50 % 7 equals 1 because 7 * 7 = 49 and remainder is 1, so result = 1.
Step 2: Calculate result += 3;
Add 3 to 1 gives result = 4.
Step 3: Calculate result *= 4;
Multiply 4 by 4 gives result = 16.
Final Answer:
16 -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.