Skip to content

Commit 5ba7c5d

Browse files
author
bluss
committed
string: Implement FromIterator<&str> and Extend<&str> for String
&str is a "particle" of a string already, see the graphemes iterator, so it seems natural that we should be able to use it with Extend.
1 parent d7d5ccf commit 5ba7c5d

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/libcollections/string.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,15 @@ impl FromIterator<char> for String {
729729
}
730730
}
731731

732+
#[experimental = "waiting on FromIterator stabilization"]
733+
impl<'a> FromIterator<&'a str> for String {
734+
fn from_iter<I:Iterator<&'a str>>(iterator: I) -> String {
735+
let mut buf = String::new();
736+
buf.extend(iterator);
737+
buf
738+
}
739+
}
740+
732741
#[experimental = "waiting on Extend stabilization"]
733742
impl Extend<char> for String {
734743
fn extend<I:Iterator<char>>(&mut self, mut iterator: I) {
@@ -740,6 +749,18 @@ impl Extend<char> for String {
740749
}
741750
}
742751

752+
#[experimental = "waiting on Extend stabilization"]
753+
impl<'a> Extend<&'a str> for String {
754+
fn extend<I: Iterator<&'a str>>(&mut self, mut iterator: I) {
755+
// A guess that at least one byte per iterator element will be needed.
756+
let (lower_bound, _) = iterator.size_hint();
757+
self.reserve(lower_bound);
758+
for s in iterator {
759+
self.push_str(s)
760+
}
761+
}
762+
}
763+
743764
impl PartialEq for String {
744765
#[inline]
745766
fn eq(&self, other: &String) -> bool { PartialEq::eq(&**self, &**other) }

0 commit comments

Comments
 (0)