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

The `+=` operator is a compound assignment operator that performs addition and assignment in a single operation. It evaluates the right-hand operand, adds it to the left-hand operand, and stores the result back into the left-hand operand by mutating it in place.

Because this operator modifies the existing memory location of the left-hand operand, Rust's borrow checker requires the left-hand operand to be a valid **mutable place expression**. This can be a variable explicitly declared with the `mut` keyword, or a dereferenced mutable reference where the reference itself does not need to be declared as `mut`.

```rust theme={"dark"}
// Mutable place expression via `mut` keyword
let mut x = 10;
x += 5; 

// Mutable place expression via dereferenced mutable reference
let mut y = 10;
let ptr: &mut i32 = &mut y;
*ptr += 5; 
```

## Trait Implementation: `std::ops::AddAssign`

In Rust, operators are syntactic sugar for trait methods. The `+=` operator is backed by the `std::ops::AddAssign` trait. For a type to support the `+=` operator, it must implement this trait.

The trait is defined in the standard library as follows:

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

When the Rust compiler encounters the `+=` syntax, compound assignment desugaring is uniform: `LHS += RHS` always desugars to `std::ops::AddAssign::add_assign(&mut LHS, RHS)`. The compiler takes a mutable borrow of the left-hand place expression.

For a simple variable `a += b`, this is equivalent to:

```rust theme={"dark"}
std::ops::AddAssign::add_assign(&mut a, b);
```

If the left-hand operand is a dereferenced pointer, such as `*ptr += b`, the compiler strictly adheres to this uniform desugaring. It does not special-case the dereference to omit the `&mut *`. Instead, it explicitly reborrows the mutable reference:

```rust theme={"dark"}
std::ops::AddAssign::add_assign(&mut *ptr, b);
```

## Technical Characteristics

**1. In-Place Mutation vs. Reallocation**
Unlike the standard addition operator `+` (backed by `std::ops::Add`), which typically returns a completely new instance of a type, `+=` takes an exclusive, mutable reference (`&mut self`) to the left operand. This allows for optimized, allocation-free updates, which is particularly relevant for heap-allocated types like `String` that can append data to their existing buffer.

**2. Return Type**
The `add_assign` method returns the unit type `()`. Consequently, the `+=` operator evaluates to `()`. This prevents assignment chaining or using the operator within larger expressions.

```rust theme={"dark"}
let mut a = 5;
let b = 2;
// let c = (a += b); // c would be of type (), not 7
```

**3. Type Heterogeneity**
The left and right operands do not strictly need to be of the same type. The `AddAssign` trait accepts a generic type parameter `Rhs` (Right-Hand Side), which defaults to `Self`. As long as an implementation exists for a specific `Self` and `Rhs` pairing, the compiler will allow the operation.

For example, the standard library implements `AddAssign<&str>` for `String`, allowing a string slice to be appended to an owned string without requiring the right operand to be an owned `String`:

```rust theme={"dark"}
let mut owned_string = String::from("Hello");
let string_slice: &str = " World";

// Valid because String implements AddAssign<&str>
owned_string += string_slice; 
```

**4. Ownership and Consumption**
The right-hand operand (`rhs`) is passed by value into the `add_assign` method. Depending on the type of `Rhs`, this means the right operand is either copied (if it implements the `Copy` trait, like integers) or consumed and moved (if it does not implement `Copy`).

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