operator

Rust ^: toggle selected bits

Integer XOR sets bits where operands differ. Applying a mask twice restores the original bits. On bool it is exclusive OR. Expected output (not executed): 0100 0110 Why: The selected bit flips and then flips back. Common mistake: `^` is not exponentiation in Rust. Advanced connection: `std::ops::BitXor`; `BitXorAssign` for `^=`.

Formal Syntax & Structure

rust syntaxrust
a ^ b

Working Example

Example Snippetrust
fn main() {    let flags = 0b0110_u8;    let mask = 0b0010_u8;    let changed = flags ^ mask;    println!("{:04b}", changed);    println!("{:04b}", changed ^ mask);}
Rust Reference