&=Operator
Explanation
&= assigns the bitwise AND of the left and right operands in place, as
in flags &= 0b1010, and is overloadable via std::ops::BitAndAssign.
Only the integer/bitwise sense of & has a compound-assignment form —
there's no analogous compound form for the borrow sense of & (borrowing
isn't a "value" that can be compounded like this).
Usage examples
Clearing bits with a mask
let mut flags = 0b1100u8;
flags &= 0b1010; // <- clears bits not 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
Clearing a single flag bit without disturbing the rest of the set is the
classic use of &=: AND the current bits against the complement of the
bit you want gone.
const FLAG_ACTIVE: u8 = 0b0001;
const FLAG_PENDING: u8 = 0b0010;
const FLAG_ARCHIVED: u8 = 0b0100;
let mut status: u8 = FLAG_ACTIVE | FLAG_PENDING | FLAG_ARCHIVED;
status &= !FLAG_PENDING; // <- clears the PENDING bit in place, keeping the others
assert_eq!(status, FLAG_ACTIVE | FLAG_ARCHIVED);
flags &= !bit is the standard bit-clearing idiom in
systems code generally, built on the
BitAndAssign
trait behind &= — see += for the fuller treatment
of compound-assignment operators shared across +=, -=, *=, and the
rest of the family.