operator

Rust &= |= ^= <<= >>=: update a bit mask

Compound bitwise assignment updates a mutable integer mask without declaring another binding. Expected output (not executed): 0010 Why: The mask changes through 0111, 0110, 0100, 1000 and 0010. Common mistake: A shift still obeys the width and overflow rules; `&=` clears bits that are zero in the mask. Advanced connection: `BitAndAssign`, `BitOrAssign`, `BitXorAssign`, `ShlAssign`, `ShrAssign`.

Formal Syntax & Structure

rust syntaxrust
place &= mask; place |= mask; place ^= mask; place <<= n; place >>= n

Working Example

Example Snippetrust
fn main() {    let mut flags = 0b0011_u8;    flags |= 0b0100;    flags &= 0b0110;    flags ^= 0b0010;    flags <<= 1;    flags >>= 2;    println!("{flags:04b}");}
Rust Reference