^=Operator
Explanation
^= assigns the bitwise XOR of the left and right operands in place,
overloadable via std::ops::BitXorAssign. A classic use is toggling bits:
flags ^= mask flips exactly the bits set in mask.
Usage examples
Toggling bits in place
let mut flags = 0b1010u8;
flags ^= 0b0011; // <- toggles the bits set in the mask, in place
Restriction: the left-hand side must be a mutable binding
(let mut) — ^= assigns in place.
Bit manipulation and flags
^= flips a specific bit without needing to know its current state
first — XOR-ing a bit in twice restores the original value.
const FLAG_NOTIFY: u8 = 0b0001;
const FLAG_MUTED: u8 = 0b0010;
let mut settings: u8 = FLAG_NOTIFY;
settings ^= FLAG_MUTED; // <- toggles the MUTED bit on, leaving NOTIFY untouched
settings ^= FLAG_MUTED; // toggling the same bit again restores the original value
assert_eq!(settings, FLAG_NOTIFY);
Toggling with ^= is the documented idiom on
BitXorAssign
for flipping a bit regardless of its current value — see
+= for the general notes shared across the
compound-assignment operator family.