Skip to content

Commit 1eaa426

Browse files
Add Add<char> and AddAssign<char> for String
This currently doesn't build! This is discussed in rust-lang#51916
1 parent 5fdcd3a commit 1eaa426

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

src/liballoc/string.rs

+39
Original file line numberDiff line numberDiff line change
@@ -1923,6 +1923,34 @@ impl<'a> Add<&'a str> for String {
19231923
}
19241924
}
19251925

1926+
/// Implements the `+` operator for concatenating a string and a single character.
1927+
///
1928+
/// This has the same behavior as the [`String::push`] method.
1929+
///
1930+
/// This consumes the `String` on the left-hand side and re-uses its buffer (growing it if
1931+
/// necessary). This is done to avoid allocating a new `String` and copying the entire contents on
1932+
/// every operation, which would lead to `O(n^2)` running time when building an `n`-byte string by
1933+
/// repeated concatenation.
1934+
///
1935+
/// # Examples
1936+
///
1937+
/// ```
1938+
/// let a = String::from("I ♥ ");
1939+
/// let b = a + '🦀';
1940+
/// // `a` is moved and can no longer be used here.
1941+
/// assert_eq(b, "I ♥ 🦀");
1942+
/// ```
1943+
#[unstable(feature = "string_plus_char", reason = "new API", issue="0")]
1944+
impl Add<char> for String {
1945+
type Output = String;
1946+
1947+
#[inline]
1948+
fn add(mut self, c: char) -> String {
1949+
self.push(c);
1950+
self
1951+
}
1952+
}
1953+
19261954
/// Implements the `+=` operator for appending to a `String`.
19271955
///
19281956
/// This has the same behavior as the [`push_str`] method.
@@ -1936,6 +1964,17 @@ impl<'a> AddAssign<&'a str> for String {
19361964
}
19371965
}
19381966

1967+
/// Implements the `+=` operator for appending a single character to a `String`.
1968+
///
1969+
/// This has the same behavior as the [`String::push`] method.
1970+
#[unstable(feature = "string_plus_char", reason = "new API", issue="0")]
1971+
impl AddAssign<char> for String {
1972+
#[inline]
1973+
fn add_assign(&mut self, c: char) {
1974+
self.push(c);
1975+
}
1976+
}
1977+
19391978
#[stable(feature = "rust1", since = "1.0.0")]
19401979
impl ops::Index<ops::Range<usize>> for String {
19411980
type Output = str;

0 commit comments

Comments
 (0)