> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Rust Bitwise XOR Assignment

The `^=` operator is the bitwise XOR (exclusive OR) assignment operator. It evaluates the bitwise XOR between a mutable left-hand operand and a right-hand operand, assigning the computed result directly back to the left-hand operand in place.

```rust theme={"dark"}
let mut a = 5;
let b = 3;

a ^= b; 
```

## Underlying Trait and Desugaring

In Rust, compound assignment operators do not desugar to their expanded assignment equivalents. The expression `a ^= b` is **not** syntactic sugar for `a = a ^ b`. Evaluating `a = a ^ b` would invoke the `BitXor::bitxor(a, b)` trait method, which takes the left operand by value and could cause a move if the type does not implement `Copy`.

Instead, `^=` is a distinct operation powered by the `std::ops::BitXorAssign` trait:

```rust theme={"dark"}
pub trait BitXorAssign<Rhs = Self> {
    fn bitxor_assign(&mut self, rhs: Rhs);
}
```

When `a ^= b` is invoked, the compiler desugars the operation directly into a method call that takes the left operand by mutable reference:

```rust theme={"dark"}
BitXorAssign::bitxor_assign(&mut a, b);
```

The Rust standard library implements this trait for all primitive integer types and booleans.

## Type-Specific Mechanics

**1. Integer Types (`u8`, `i32`, `usize`, etc.)**
When applied to integers, the operator performs a parallel, bit-by-bit comparison of the binary representations of both operands. For each bit position:

* If the bits are different (`1` and `0`, or `0` and `1`), the resulting bit is `1`.
* If the bits are identical (`1` and `1`, or `0` and `0`), the resulting bit is `0`.

```rust theme={"dark"}
let mut x: u8 = 0b1010_1100; // Decimal: 172
let y: u8     = 0b0110_0101; // Decimal: 101

x ^= y; 

// Calculation:
//   1010_1100
// ^ 0110_0101
// --------
//   1100_1001 (Decimal: 201)
```

**2. Boolean Types (`bool`)**
When applied to `bool` primitives, `^=` acts as a strict logical inequality assignment. The left-hand operand evaluates to `true` if the operands possess different truth values, and `false` if they share the same truth value.

```rust theme={"dark"}
let mut state = true;

state ^= false; // state is true  (true ^ false = true)
state ^= true;  // state is false (true ^ true = false)
state ^= false; // state is false (false ^ false = false)
```

## Mutability and Evaluation

Because `^=` mutates the left-hand operand in place, the variable must be bound with the `mut` keyword. Furthermore, unlike logical operators (`&&`, `||`), bitwise operators do not short-circuit. Both the left and right operands are fully evaluated before the XOR assignment occurs.

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Rust Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
