Skip to content

Commit 20a8412

Browse files
committed
Auto merge of #27956 - withoutboats:extend_string, r=alexcrichton
If you have an `Iterator<Item=String>` (say because those items were generated using `.to_string()` or similarly), borrow semantics do not permit you map that to an `Iterator<&'a str>`. These two implementations close a small gap in the `String` API. At the same time I've also made the names of the parameters to `String`'s `Extend` and `FromIterator` implementations consistent.
2 parents 8dba06a + 8af543a commit 20a8412

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

src/libcollections/string.rs

+24-6
Original file line numberDiff line numberDiff line change
@@ -768,18 +768,27 @@ impl fmt::Display for FromUtf16Error {
768768

769769
#[stable(feature = "rust1", since = "1.0.0")]
770770
impl FromIterator<char> for String {
771-
fn from_iter<I: IntoIterator<Item=char>>(iter: I) -> String {
771+
fn from_iter<I: IntoIterator<Item=char>>(iterable: I) -> String {
772772
let mut buf = String::new();
773-
buf.extend(iter);
773+
buf.extend(iterable);
774774
buf
775775
}
776776
}
777777

778778
#[stable(feature = "rust1", since = "1.0.0")]
779779
impl<'a> FromIterator<&'a str> for String {
780-
fn from_iter<I: IntoIterator<Item=&'a str>>(iter: I) -> String {
780+
fn from_iter<I: IntoIterator<Item=&'a str>>(iterable: I) -> String {
781781
let mut buf = String::new();
782-
buf.extend(iter);
782+
buf.extend(iterable);
783+
buf
784+
}
785+
}
786+
787+
#[stable(feature = "extend_string", since = "1.4.0")]
788+
impl FromIterator<String> for String {
789+
fn from_iter<I: IntoIterator<Item=String>>(iterable: I) -> String {
790+
let mut buf = String::new();
791+
buf.extend(iterable);
783792
buf
784793
}
785794
}
@@ -798,8 +807,8 @@ impl Extend<char> for String {
798807

799808
#[stable(feature = "extend_ref", since = "1.2.0")]
800809
impl<'a> Extend<&'a char> for String {
801-
fn extend<I: IntoIterator<Item=&'a char>>(&mut self, iter: I) {
802-
self.extend(iter.into_iter().cloned());
810+
fn extend<I: IntoIterator<Item=&'a char>>(&mut self, iterable: I) {
811+
self.extend(iterable.into_iter().cloned());
803812
}
804813
}
805814

@@ -812,6 +821,15 @@ impl<'a> Extend<&'a str> for String {
812821
}
813822
}
814823

824+
#[stable(feature = "extend_string", since = "1.4.0")]
825+
impl Extend<String> for String {
826+
fn extend<I: IntoIterator<Item=String>>(&mut self, iterable: I) {
827+
for s in iterable {
828+
self.push_str(&s)
829+
}
830+
}
831+
}
832+
815833
/// A convenience impl that delegates to the impl for `&str`
816834
impl<'a, 'b> Pattern<'a> for &'b String {
817835
type Searcher = <&'b str as Pattern<'a>>::Searcher;

0 commit comments

Comments
 (0)