Skip to content

Commit 52dbe13

Browse files
committed
auto merge of #8100 : blake2-ppc/rust/str-collect, r=pcwalton
FromIterator initially only implemented for Iterator<char>, which is the type of the main iterator.
2 parents 4cc3bbb + 4849a42 commit 52dbe13

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/libstd/str.rs

+23-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use char::Char;
2323
use clone::Clone;
2424
use container::{Container, Mutable};
2525
use iter::Times;
26-
use iterator::{Iterator, IteratorUtil, FilterIterator, AdditiveIterator, MapIterator};
26+
use iterator::{Iterator, FromIterator, IteratorUtil, FilterIterator, AdditiveIterator, MapIterator};
2727
use libc;
2828
use num::Zero;
2929
use option::{None, Option, Some};
@@ -2319,6 +2319,18 @@ impl<'self> Iterator<u8> for BytesRevIterator<'self> {
23192319
}
23202320
}
23212321
2322+
impl<T: Iterator<char>> FromIterator<char, T> for ~str {
2323+
#[inline]
2324+
fn from_iterator(iterator: &mut T) -> ~str {
2325+
let (lower, _) = iterator.size_hint();
2326+
let mut buf = with_capacity(lower);
2327+
for iterator.advance |ch| {
2328+
buf.push_char(ch)
2329+
}
2330+
buf
2331+
}
2332+
}
2333+
23222334
// This works because every lifetime is a sub-lifetime of 'static
23232335
impl<'self> Zero for &'self str {
23242336
fn zero() -> &'self str { "" }
@@ -2482,6 +2494,16 @@ mod tests {
24822494
assert_eq!(~"华ประเทศไทย中", data);
24832495
}
24842496
2497+
#[test]
2498+
fn test_collect() {
2499+
let empty = "";
2500+
let s: ~str = empty.iter().collect();
2501+
assert_eq!(empty, s.as_slice());
2502+
let data = "ประเทศไทย中";
2503+
let s: ~str = data.iter().collect();
2504+
assert_eq!(data, s.as_slice());
2505+
}
2506+
24852507
#[test]
24862508
fn test_clear() {
24872509
let mut empty = ~"";

0 commit comments

Comments
 (0)