From 180dcc3118998ebbe390f876df51f85f1b33407a Mon Sep 17 00:00:00 2001 From: Nathan West Date: Wed, 5 Dec 2018 14:37:38 -0800 Subject: [PATCH] Optimized string FromIterator impls --- src/liballoc/string.rs | 41 +++++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/src/liballoc/string.rs b/src/liballoc/string.rs index 662f8ae614fcb..ef9a34ec611c6 100644 --- a/src/liballoc/string.rs +++ b/src/liballoc/string.rs @@ -1732,18 +1732,31 @@ impl<'a> FromIterator<&'a str> for String { #[stable(feature = "extend_string", since = "1.4.0")] impl FromIterator for String { fn from_iter>(iter: I) -> String { - let mut buf = String::new(); - buf.extend(iter); - buf + let iterator = iter.into_iter(); + + match iterator.next() { + None => String::new(), + Some(buf) => { + buf.extend(iterator); + buf + } + } } } #[stable(feature = "herd_cows", since = "1.19.0")] impl<'a> FromIterator> for String { fn from_iter>>(iter: I) -> String { - let mut buf = String::new(); - buf.extend(iter); - buf + let iterator = iter.into_iter(); + + match iterator.next() { + None => String::new(), + Some(cow) => { + let buf = cow.into_owned(); + buf.extend(iterator); + buf + } + } } } @@ -1753,9 +1766,7 @@ impl Extend for String { let iterator = iter.into_iter(); let (lower_bound, _) = iterator.size_hint(); self.reserve(lower_bound); - for ch in iterator { - self.push(ch) - } + iterator.for_each(move |c| self.push(c)); } } @@ -1769,27 +1780,21 @@ impl<'a> Extend<&'a char> for String { #[stable(feature = "rust1", since = "1.0.0")] impl<'a> Extend<&'a str> for String { fn extend>(&mut self, iter: I) { - for s in iter { - self.push_str(s) - } + iter.into_iter().for_each(move |s| self.push_str(s)); } } #[stable(feature = "extend_string", since = "1.4.0")] impl Extend for String { fn extend>(&mut self, iter: I) { - for s in iter { - self.push_str(&s) - } + iter.into_iter().for_each(move |s| self.push_str(&s)); } } #[stable(feature = "herd_cows", since = "1.19.0")] impl<'a> Extend> for String { fn extend>>(&mut self, iter: I) { - for s in iter { - self.push_str(&s) - } + iter.into_iter().for_each(move |s| self.push_str(&s)); } }