Skip to content

std: Implement FromIterator for ~str #8100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 29, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion src/libstd/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use char::Char;
use clone::Clone;
use container::{Container, Mutable};
use iter::Times;
use iterator::{Iterator, IteratorUtil, FilterIterator, AdditiveIterator, MapIterator};
use iterator::{Iterator, FromIterator, IteratorUtil, FilterIterator, AdditiveIterator, MapIterator};
use libc;
use num::Zero;
use option::{None, Option, Some};
Expand Down Expand Up @@ -2319,6 +2319,18 @@ impl<'self> Iterator<u8> for BytesRevIterator<'self> {
}
}

impl<T: Iterator<char>> FromIterator<char, T> for ~str {
#[inline]
fn from_iterator(iterator: &mut T) -> ~str {
let (lower, _) = iterator.size_hint();
let mut buf = with_capacity(lower);
for iterator.advance |ch| {
buf.push_char(ch)
}
buf
}
}

// This works because every lifetime is a sub-lifetime of 'static
impl<'self> Zero for &'self str {
fn zero() -> &'self str { "" }
Expand Down Expand Up @@ -2482,6 +2494,16 @@ mod tests {
assert_eq!(~"华ประเทศไทย中", data);
}

#[test]
fn test_collect() {
let empty = "";
let s: ~str = empty.iter().collect();
assert_eq!(empty, s.as_slice());
let data = "ประเทศไทย中";
let s: ~str = data.iter().collect();
assert_eq!(data, s.as_slice());
}

#[test]
fn test_clear() {
let mut empty = ~"";
Expand Down