Skip to content

Commit 4817c5e

Browse files
Rollup merge of rust-lang#34890 - oconnor663:addassign, r=brson
implement AddAssign for String Currently `String` implements `Add` but not `AddAssign`. This PR fills in that gap. I played around with having `AddAssign` (and `Add` and `push_str`) take `AsRef<str>` instead of `&str`, but it looks like that breaks arguments that implement `Deref<Target=str>` and not `AsRef<str>`. Comments in [`libcore/convert.rs`](https://github.com/rust-lang/rust/blob/master/src/libcore/convert.rs#L207-L213) make it sound like we could fix this with a blanket impl eventually. Does anyone know what's blocking that?
2 parents 9ba1792 + 9b81306 commit 4817c5e

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/libcollections/string.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ use core::fmt;
5959
use core::hash;
6060
use core::iter::FromIterator;
6161
use core::mem;
62-
use core::ops::{self, Add, Index, IndexMut};
62+
use core::ops::{self, Add, AddAssign, Index, IndexMut};
6363
use core::ptr;
6464
use core::str::pattern::Pattern;
6565
use rustc_unicode::char::{decode_utf16, REPLACEMENT_CHARACTER};
@@ -1565,6 +1565,14 @@ impl<'a> Add<&'a str> for String {
15651565
}
15661566
}
15671567

1568+
#[stable(feature = "stringaddassign", since = "1.12.0")]
1569+
impl<'a> AddAssign<&'a str> for String {
1570+
#[inline]
1571+
fn add_assign(&mut self, other: &str) {
1572+
self.push_str(other);
1573+
}
1574+
}
1575+
15681576
#[stable(feature = "rust1", since = "1.0.0")]
15691577
impl ops::Index<ops::Range<usize>> for String {
15701578
type Output = str;

0 commit comments

Comments
 (0)