@@ -1629,6 +1629,43 @@ impl hash::Hash for String {
16291629 }
16301630}
16311631
1632+ /// Implements the `+` operator for concatenating two strings.
1633+ ///
1634+ /// This consumes the `String` on the left-hand side and re-uses its buffer (growing it if
1635+ /// necessary). This is done to avoid allocating a new `String` and copying the entire contents on
1636+ /// every operation, which would lead to `O(n^2)` running time when building an `n`-byte string by
1637+ /// repeated concatenation.
1638+ ///
1639+ /// The string on the right-hand side is only borrowed; its contents are copied into the returned
1640+ /// `String`.
1641+ ///
1642+ /// # Examples
1643+ ///
1644+ /// Concatenating two `String`s takes the first by value and borrows the second:
1645+ ///
1646+ /// ```
1647+ /// let a = String::from("hello");
1648+ /// let b = String::from(" world");
1649+ /// let c = a + &b;
1650+ /// // `a` is moved and can no longer be used here.
1651+ /// ```
1652+ ///
1653+ /// If you want to keep using the first `String`, you can clone it and append to the clone instead:
1654+ ///
1655+ /// ```
1656+ /// let a = String::from("hello");
1657+ /// let b = String::from(" world");
1658+ /// let c = a.clone() + &b;
1659+ /// // `a` is still valid here.
1660+ /// ```
1661+ ///
1662+ /// Concatenating `&str` slices can be done by converting the first to a `String`:
1663+ ///
1664+ /// ```
1665+ /// let a = "hello";
1666+ /// let b = " world";
1667+ /// let c = a.to_string() + b;
1668+ /// ```
16321669#[ stable( feature = "rust1" , since = "1.0.0" ) ]
16331670impl < ' a > Add < & ' a str > for String {
16341671 type Output = String ;
@@ -1640,6 +1677,11 @@ impl<'a> Add<&'a str> for String {
16401677 }
16411678}
16421679
1680+ /// Implements the `+=` operator for appending to a `String`.
1681+ ///
1682+ /// This has the same behavior as the [`push_str()`] method.
1683+ ///
1684+ /// [`push_str()`]: struct.String.html#method.push_str
16431685#[ stable( feature = "stringaddassign" , since = "1.12.0" ) ]
16441686impl < ' a > AddAssign < & ' a str > for String {
16451687 #[ inline]
0 commit comments