Skip to content

Commit

Permalink
Auto merge of #56463 - ljedrz:slice_concat_join, r=nikic
Browse files Browse the repository at this point in the history
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
}
```
  • Loading branch information
bors committed Dec 9, 2018
2 parents b755501 + ae53273 commit 9cb38a8
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/liballoc/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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 {
Expand Down

0 comments on commit 9cb38a8

Please sign in to comment.