diff options
| author | bluss <bluss> | 2014-12-07 21:32:00 +0100 |
|---|---|---|
| committer | bluss <bluss> | 2014-12-07 22:45:27 +0100 |
| commit | 5ba7c5da62bae732bf466191dbeb5f699ba44d70 (patch) | |
| tree | 4a9761541e8970c309b4ed4865827da525b40122 /src/libcollections | |
| parent | d7d5ccf9bb3bc63f347d8d5441092f20a3fe7088 (diff) | |
| download | rust-5ba7c5da62bae732bf466191dbeb5f699ba44d70.tar.gz rust-5ba7c5da62bae732bf466191dbeb5f699ba44d70.zip | |
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.
Diffstat (limited to 'src/libcollections')
| -rw-r--r-- | src/libcollections/string.rs | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/src/libcollections/string.rs b/src/libcollections/string.rs index 961fca0f02b..ed578f3f515 100644 --- a/src/libcollections/string.rs +++ b/src/libcollections/string.rs @@ -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) { @@ -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) } |
