diff options
| author | king6cong <king6cong@gmail.com> | 2019-12-24 11:59:57 +0800 |
|---|---|---|
| committer | king6cong <king6cong@gmail.com> | 2019-12-24 12:44:05 +0800 |
| commit | 3c56a65bc42fa83edbcdfdb5357e99c5557755cb (patch) | |
| tree | 3b5365ca7d6685d267a7b8c7c0552b21a69f2b17 /src/liballoc/slice.rs | |
| parent | a4cd03dee2b57216b5c95084a0b46de130946ad7 (diff) | |
| download | rust-3c56a65bc42fa83edbcdfdb5357e99c5557755cb.tar.gz rust-3c56a65bc42fa83edbcdfdb5357e99c5557755cb.zip | |
reuse `capacity` variable in slice::repeat
Diffstat (limited to 'src/liballoc/slice.rs')
| -rw-r--r-- | src/liballoc/slice.rs | 8 |
1 files changed, 4 insertions, 4 deletions
diff --git a/src/liballoc/slice.rs b/src/liballoc/slice.rs index 2f6d10c027b..7b83658fca6 100644 --- a/src/liballoc/slice.rs +++ b/src/liballoc/slice.rs @@ -450,7 +450,8 @@ impl<T> [T] { // and `rem` is the remaining part of `n`. // Using `Vec` to access `set_len()`. - let mut buf = Vec::with_capacity(self.len().checked_mul(n).expect("capacity overflow")); + let capacity = self.len().checked_mul(n).expect("capacity overflow"); + let mut buf = Vec::with_capacity(capacity); // `2^expn` repetition is done by doubling `buf` `expn`-times. buf.extend(self); @@ -476,7 +477,7 @@ impl<T> [T] { // `rem` (`= n - 2^expn`) repetition is done by copying // first `rem` repetitions from `buf` itself. - let rem_len = self.len() * n - buf.len(); // `self.len() * rem` + let rem_len = capacity - buf.len(); // `self.len() * rem` if rem_len > 0 { // `buf.extend(buf[0 .. rem_len])`: unsafe { @@ -487,8 +488,7 @@ impl<T> [T] { rem_len, ); // `buf.len() + rem_len` equals to `buf.capacity()` (`= self.len() * n`). - let buf_cap = buf.capacity(); - buf.set_len(buf_cap); + buf.set_len(capacity); } } buf |
