Skip to main content

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.

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.
// 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:
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:
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:
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.
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:
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).
Master Rust with Deep Grasping Methodology!Learn More