Skip to main content
The + operator in Rust serves two distinct syntactic roles: as an algebraic operator that desugars to the std::ops::Add trait for arithmetic and concatenation, and as a type-system operator for specifying compound trait and lifetime bounds.

The std::ops::Add Trait

When used as a binary operator between values (LHS + RHS), the + symbol is syntactic sugar for the add method of the std::ops::Add trait. The compiler translates a + b into a.add(b). The trait is defined in the standard library as follows:
  • Rhs (Right-Hand Side): A generic type parameter that defaults to Self. This allows the right operand to be a different type than the left operand.
  • Output: An associated type defining the type returned by the addition operation.
  • Ownership: The add method takes ownership of self (the left operand) by value. Whether rhs is consumed or borrowed depends on the specific type implementation.
To enable the + operator for custom types, you must implement this trait:

String Concatenation Mechanics

When applied to strings, the + operator relies on a specific implementation of the Add trait: impl Add<&str> for String.
Mechanically, this operation:
  1. Takes ownership of s1 (self), meaning s1 is moved and can no longer be used.
  2. Takes a string slice (&str) for the right operand.
  3. Appends the contents of the right operand to the left operand’s memory buffer.
  4. Returns the reused, now-concatenated String.
If the right operand is a &String (as in &s2), Rust applies deref coercion to convert &String into &str (&s2[..]) to satisfy the trait signature.

Compound Trait and Lifetime Bounds

In the context of the type system, the + operator acts as a logical AND (conjunction) for constraining generic types, opaque types (impl Trait), and trait objects (dyn Trait). Multiple Trait Bounds: It restricts a generic type parameter to types that implement all specified traits.
Lifetime Bounds: It binds a type or trait object to a specific lifetime, ensuring that any references held within the type outlive the specified lifetime.
Tired of Poor Rust Skills? Fix That With Deep Grasping!Learn More