diff options
| author | Mazdak Farrokhzad <twingoow@gmail.com> | 2019-04-06 22:52:46 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-04-06 22:52:46 +0200 |
| commit | 76d818cf73cd49f09f0dede8d06214d8fda833b4 (patch) | |
| tree | e61696cb41473e3f578ebb8bb850d77c196d1662 /src/libstd | |
| parent | ed45788e532449e27411e9ccebecc36562c4bbcf (diff) | |
| parent | 0730a01c5ca3b0e8760d72a05c47d4199bd64728 (diff) | |
| download | rust-76d818cf73cd49f09f0dede8d06214d8fda833b4.tar.gz rust-76d818cf73cd49f09f0dede8d06214d8fda833b4.zip | |
Rollup merge of #59740 - cuviper:folded-extend, r=scottmcm
Use for_each to extend collections This updates the `Extend` implementations to use `for_each` for many collections: `BinaryHeap`, `BTreeMap`, `BTreeSet`, `LinkedList`, `Path`, `TokenStream`, `VecDeque`, and `Wtf8Buf`. Folding with `for_each` enables better performance than a `for`-loop for some iterators, especially if they can just forward to internal iterators, like `Chain` and `FlatMap` do.
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/path.rs | 4 | ||||
| -rw-r--r-- | src/libstd/sys_common/wtf8.rs | 4 |
2 files changed, 2 insertions, 6 deletions
diff --git a/src/libstd/path.rs b/src/libstd/path.rs index 71e82f0a9b0..1bbda9b5bcb 100644 --- a/src/libstd/path.rs +++ b/src/libstd/path.rs @@ -1551,9 +1551,7 @@ impl<P: AsRef<Path>> iter::FromIterator<P> for PathBuf { #[stable(feature = "rust1", since = "1.0.0")] impl<P: AsRef<Path>> iter::Extend<P> for PathBuf { fn extend<I: IntoIterator<Item = P>>(&mut self, iter: I) { - for p in iter { - self.push(p.as_ref()) - } + iter.into_iter().for_each(move |p| self.push(p.as_ref())); } } diff --git a/src/libstd/sys_common/wtf8.rs b/src/libstd/sys_common/wtf8.rs index 7fe1818291e..f17020de44e 100644 --- a/src/libstd/sys_common/wtf8.rs +++ b/src/libstd/sys_common/wtf8.rs @@ -388,9 +388,7 @@ impl Extend<CodePoint> for Wtf8Buf { let (low, _high) = iterator.size_hint(); // Lower bound of one byte per code point (ASCII only) self.bytes.reserve(low); - for code_point in iterator { - self.push(code_point); - } + iterator.for_each(move |code_point| self.push(code_point)); } } |
