about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2018-12-09 22:39:44 +0000
committerbors <bors@rust-lang.org>2018-12-09 22:39:44 +0000
commit9cb38a84e7d593106946cae8e25d9cdbf24751ee (patch)
tree4be9fd3515502a4f151504332fcbb156b6707206 /src
parentb755501043d5b27b39f94bcadd57c8d5dedfd6ba (diff)
parentae532730215f1e74b175d1aa214146e9d900e5fd (diff)
downloadrust-9cb38a84e7d593106946cae8e25d9cdbf24751ee.tar.gz
rust-9cb38a84e7d593106946cae8e25d9cdbf24751ee.zip
Auto merge of #56463 - ljedrz:slice_concat_join, r=nikic
slice: tweak concat & join

- use `sum` instead of `fold` (readability)
- adjust the capacity for `join` - the number of separators is `n - 1`, not `n`; proof:
```
fn main() {
    let a = [[1, 2], [4, 5]];
    let v = a.join(&3);

    assert_ne!(v.len(), v.capacity()); // len is 5, capacity is 6
}
```
Diffstat (limited to 'src')
-rw-r--r--src/liballoc/slice.rs6
1 files changed, 3 insertions, 3 deletions
diff --git a/src/liballoc/slice.rs b/src/liballoc/slice.rs
index a8e9a2f5a39..510b4b06e40 100644
--- a/src/liballoc/slice.rs
+++ b/src/liballoc/slice.rs
@@ -589,7 +589,7 @@ impl<T: Clone, V: Borrow<[T]>> SliceConcatExt<T> for [V] {
     type Output = Vec<T>;
 
     fn concat(&self) -> Vec<T> {
-        let size = self.iter().fold(0, |acc, v| acc + v.borrow().len());
+        let size = self.iter().map(|slice| slice.borrow().len()).sum();
         let mut result = Vec::with_capacity(size);
         for v in self {
             result.extend_from_slice(v.borrow())
@@ -603,8 +603,8 @@ impl<T: Clone, V: Borrow<[T]>> SliceConcatExt<T> for [V] {
             Some(first) => first,
             None => return vec![],
         };
-        let size = self.iter().fold(0, |acc, v| acc + v.borrow().len());
-        let mut result = Vec::with_capacity(size + self.len());
+        let size = self.iter().map(|slice| slice.borrow().len()).sum::<usize>() + self.len() - 1;
+        let mut result = Vec::with_capacity(size);
         result.extend_from_slice(first.borrow());
 
         for v in iter {