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 } ```
This commit is contained in:
commit
9cb38a84e7
@ -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 {
|
||||
|
Loading…
x
Reference in New Issue
Block a user