Recall & Review
beginner
What is the basic assignment operator in Rust?
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?✗ 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?
✗ Incorrect
++= is not a valid operator in Rust. Rust does not have increment (++) operators.What is the result of this code?<br>
let mut a = 10; a /= 2;✗ 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?✗ 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?
✗ 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.