0
0
Rustprogramming~5 mins

Assignment operators in Rust - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
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
Which of these is NOT a valid compound assignment operator in Rust?
A*=
B+=
C++=
D&=
What is the result of this code?<br>let mut a = 10; a /= 2;
Aa is 5
Ba is 20
Ca is 2
DCompilation error
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
Which operator would you use to perform a bitwise AND and assign the result to a variable?
A&=
B|=
C^=
D&&=
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.