> ## 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 AND Assignment

The `&=` operator is a compound assignment operator that performs a bitwise AND operation (or logical AND for booleans) between a mutable left-hand side (LHS) operand and a right-hand side (RHS) operand, updating the LHS operand in place with the result.

Unlike some languages where `lhs &= rhs` is syntactic sugar for `lhs = lhs & rhs`, Rust treats compound assignment as a distinct operation. The `&=` operator desugars directly to a method call on the `std::ops::BitAndAssign` trait.

```rust theme={"dark"}
// Standard syntax
lhs &= rhs;

// Desugared equivalent
std::ops::BitAndAssign::bitand_assign(&mut lhs, rhs);
```

This distinction is critical in Rust's ownership and type system:

* **Borrowing vs. Moving:** `lhs &= rhs` only borrows `lhs` mutably. Conversely, `lhs = lhs & rhs` consumes (moves) `lhs` if the type does not implement the `Copy` trait.
* **Trait Independence:** A type can implement `BitAndAssign` without implementing the `BitAnd` trait. Therefore, `lhs &= rhs` can be perfectly valid even when `lhs = lhs & rhs` results in a compiler error.

## Technical Mechanics

At the bit level, the operator compares each corresponding bit of the LHS and RHS operands. If both bits are `1`, the resulting bit is `1`. Otherwise, the resulting bit is `0`.

When applied to boolean types (`bool`), `&=` functions as a logical AND assignment, evaluating to `true` only if both the current LHS value and the RHS value are `true`.

The LHS variable must be explicitly declared as mutable (`mut`) for the compiler to allow the assignment.

```rust theme={"dark"}
// Integer bitwise AND assignment
let mut a: u8 = 0b1100_1100;
let b: u8     = 0b1010_1010;

a &= b; 

assert_eq!(a, 0b1000_1000);

// Boolean AND assignment
let mut flag = true;
flag &= false;

assert_eq!(flag, false);
```

## Trait Implementation

The Rust standard library implements the `BitAndAssign` trait for all primitive integer types and booleans.

To use `&=` with custom structs or enums, you must explicitly implement `std::ops::BitAndAssign` for your type. The trait requires the implementation of a single method, `bitand_assign`, which takes a mutable reference to `self` and consumes the RHS operand.

```rust theme={"dark"}
use std::ops::BitAndAssign;

#[derive(Debug, PartialEq)]
struct BitVector(u16);

impl BitAndAssign for BitVector {
    fn bitand_assign(&mut self, rhs: Self) {
        // Mutate self in place
        self.0 &= rhs.0;
    }
}

let mut v1 = BitVector(0b1111);
let v2 = BitVector(0b0101);

v1 &= v2;

assert_eq!(v1, BitVector(0b0101));
```

<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>
