@@ -1982,6 +1982,91 @@ impl Add<&str> for String {
1982
1982
}
1983
1983
}
1984
1984
1985
+ // This had to be added to avoid breakage after adding `impl Add<char> for String`
1986
+ #[ allow( missing_docs) ]
1987
+ #[ stable( feature = "extra_add_string_and_dbl_ref_str" , since = "1.41.0" ) ]
1988
+ impl Add < & & str > for String {
1989
+ type Output = String ;
1990
+
1991
+ #[ inline]
1992
+ fn add ( mut self , other : & & str ) -> String {
1993
+ self . push_str ( other) ;
1994
+ self
1995
+ }
1996
+ }
1997
+
1998
+ // This had to be added to avoid breakage after adding `impl Add<char> for String`
1999
+ #[ allow( missing_docs) ]
2000
+ #[ stable( feature = "extra_add_string_and_ref_string" , since = "1.41.0" ) ]
2001
+ impl Add < & String > for String {
2002
+ type Output = String ;
2003
+
2004
+ #[ inline]
2005
+ fn add ( mut self , other : & String ) -> String {
2006
+ self . push_str ( other) ;
2007
+ self
2008
+ }
2009
+ }
2010
+
2011
+ // This had to be added to avoid breakage after adding `impl Add<char> for String`
2012
+ #[ allow( missing_docs) ]
2013
+ #[ stable( feature = "extra_add_string_and_dbl_ref_string" , since = "1.41.0" ) ]
2014
+ impl Add < & & String > for String {
2015
+ type Output = String ;
2016
+
2017
+ #[ inline]
2018
+ fn add ( mut self , other : & & String ) -> String {
2019
+ self . push_str ( other) ;
2020
+ self
2021
+ }
2022
+ }
2023
+
2024
+ /// Implements the `+` operator for concatenating a string and a char together.
2025
+ ///
2026
+ /// This consumes the `String` on the left-hand side and re-uses its buffer (growing it if
2027
+ /// necessary). This is done to avoid allocating a new `String` and copying the entire contents on
2028
+ /// every operation, which would lead to `O(n^2)` running time when building an `n`-byte string by
2029
+ /// repeated concatenation.
2030
+ ///
2031
+ /// # Examples
2032
+ ///
2033
+ /// Concatenating a `String` with a `char` takes the `String` by value and copies the `char`:
2034
+ ///
2035
+ /// ```
2036
+ /// let a = String::from("hello world! ");
2037
+ /// let b = '👋';
2038
+ /// let c = a + b;
2039
+ /// // `a` is moved and can no longer be used here.
2040
+ /// ```
2041
+ ///
2042
+ /// If you want to keep using the initial `String`, you can clone it and append to the
2043
+ /// clone instead:
2044
+ ///
2045
+ /// ```
2046
+ /// let a = String::from("hello world! ");
2047
+ /// let b = '👋';
2048
+ /// let c = a.clone() + b;
2049
+ /// // `a` is still valid here.
2050
+ /// ```
2051
+ ///
2052
+ /// Concatenating `char` to a `&str` slice can be done by converting the `&str` to a `String`:
2053
+ ///
2054
+ /// ```
2055
+ /// let a = "hello world! ";
2056
+ /// let b = '👋';
2057
+ /// let c = a.to_string() + b;
2058
+ /// ```
2059
+ #[ stable( feature = "add_string_and_char" , since = "1.41.0" ) ]
2060
+ impl Add < char > for String {
2061
+ type Output = String ;
2062
+
2063
+ #[ inline]
2064
+ fn add ( mut self , other : char ) -> String {
2065
+ self . push ( other) ;
2066
+ self
2067
+ }
2068
+ }
2069
+
1985
2070
/// Implements the `+=` operator for appending to a `String`.
1986
2071
///
1987
2072
/// This has the same behavior as the [`push_str`][String::push_str] method.
@@ -1993,6 +2078,28 @@ impl AddAssign<&str> for String {
1993
2078
}
1994
2079
}
1995
2080
2081
+ /// Implements the `+=` operator for appending to a `String`.
2082
+ ///
2083
+ /// This has the same behavior as the [`push_str`][String::push_str] method.
2084
+ #[ stable( feature = "string_add_assign_string" , since = "1.41.0" ) ]
2085
+ impl AddAssign < & String > for String {
2086
+ #[ inline]
2087
+ fn add_assign ( & mut self , other : & String ) {
2088
+ self . push_str ( other) ;
2089
+ }
2090
+ }
2091
+
2092
+ /// Implements the `+=` operator for appending a `char` to a `String`.
2093
+ ///
2094
+ /// This has the same behavior as the [`push`][String::push] method.
2095
+ #[ stable( feature = "string_add_assign_char" , since = "1.41.0" ) ]
2096
+ impl AddAssign < char > for String {
2097
+ #[ inline]
2098
+ fn add_assign ( & mut self , other : char ) {
2099
+ self . push ( other) ;
2100
+ }
2101
+ }
2102
+
1996
2103
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1997
2104
impl ops:: Index < ops:: Range < usize > > for String {
1998
2105
type Output = str ;
0 commit comments