diff options
| author | bors <bors@rust-lang.org> | 2015-08-28 01:06:51 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2015-08-28 01:06:51 +0000 |
| commit | 20a8412e09e2883489736b4ae3dc138f243d1a17 (patch) | |
| tree | 356d1f2da5de0ed43f7cca3f7be522236fa1b3e0 | |
| parent | 8dba06aeee28e9ed6a1b9918a91cbef242af53d3 (diff) | |
| parent | 8af543af5d4588a5b9d32fd01e8888fcf3e258aa (diff) | |
| download | rust-20a8412e09e2883489736b4ae3dc138f243d1a17.tar.gz rust-20a8412e09e2883489736b4ae3dc138f243d1a17.zip | |
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.
| -rw-r--r-- | src/libcollections/string.rs | 30 |
1 files changed, 24 insertions, 6 deletions
diff --git a/src/libcollections/string.rs b/src/libcollections/string.rs index 08ac64778bb..5109261a504 100644 --- a/src/libcollections/string.rs +++ b/src/libcollections/string.rs @@ -768,18 +768,27 @@ impl fmt::Display for FromUtf16Error { #[stable(feature = "rust1", since = "1.0.0")] impl FromIterator<char> for String { - fn from_iter<I: IntoIterator<Item=char>>(iter: I) -> String { + fn from_iter<I: IntoIterator<Item=char>>(iterable: I) -> String { let mut buf = String::new(); - buf.extend(iter); + buf.extend(iterable); buf } } #[stable(feature = "rust1", since = "1.0.0")] impl<'a> FromIterator<&'a str> for String { - fn from_iter<I: IntoIterator<Item=&'a str>>(iter: I) -> String { + fn from_iter<I: IntoIterator<Item=&'a str>>(iterable: I) -> String { let mut buf = String::new(); - buf.extend(iter); + buf.extend(iterable); + buf + } +} + +#[stable(feature = "extend_string", since = "1.4.0")] +impl FromIterator<String> for String { + fn from_iter<I: IntoIterator<Item=String>>(iterable: I) -> String { + let mut buf = String::new(); + buf.extend(iterable); buf } } @@ -798,8 +807,8 @@ impl Extend<char> for String { #[stable(feature = "extend_ref", since = "1.2.0")] impl<'a> Extend<&'a char> for String { - fn extend<I: IntoIterator<Item=&'a char>>(&mut self, iter: I) { - self.extend(iter.into_iter().cloned()); + fn extend<I: IntoIterator<Item=&'a char>>(&mut self, iterable: I) { + self.extend(iterable.into_iter().cloned()); } } @@ -812,6 +821,15 @@ impl<'a> Extend<&'a str> for String { } } +#[stable(feature = "extend_string", since = "1.4.0")] +impl Extend<String> for String { + fn extend<I: IntoIterator<Item=String>>(&mut self, iterable: I) { + for s in iterable { + self.push_str(&s) + } + } +} + /// A convenience impl that delegates to the impl for `&str` impl<'a, 'b> Pattern<'a> for &'b String { type Searcher = <&'b str as Pattern<'a>>::Searcher; |
