Navigation Menu

Skip to content

Commit

Permalink
string: Implement FromIterator<&str> and Extend<&str> for String
Browse files Browse the repository at this point in the history
&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.
  • Loading branch information
bluss committed Dec 7, 2014
1 parent d7d5ccf commit 5ba7c5d
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/libcollections/string.rs
Expand Up @@ -729,6 +729,15 @@ impl FromIterator<char> for String {
}
}

#[experimental = "waiting on FromIterator stabilization"]
impl<'a> FromIterator<&'a str> for String {
fn from_iter<I:Iterator<&'a str>>(iterator: I) -> String {
let mut buf = String::new();
buf.extend(iterator);
buf
}
}

#[experimental = "waiting on Extend stabilization"]
impl Extend<char> for String {
fn extend<I:Iterator<char>>(&mut self, mut iterator: I) {
Expand All @@ -740,6 +749,18 @@ impl Extend<char> for String {
}
}

#[experimental = "waiting on Extend stabilization"]
impl<'a> Extend<&'a str> for String {
fn extend<I: Iterator<&'a str>>(&mut self, mut iterator: I) {
// A guess that at least one byte per iterator element will be needed.
let (lower_bound, _) = iterator.size_hint();
self.reserve(lower_bound);
for s in iterator {
self.push_str(s)
}
}
}

impl PartialEq for String {
#[inline]
fn eq(&self, other: &String) -> bool { PartialEq::eq(&**self, &**other) }
Expand Down

0 comments on commit 5ba7c5d

Please sign in to comment.