diff options
| author | Ben Kimock <kimockb@gmail.com> | 2022-07-13 20:46:40 -0400 |
|---|---|---|
| committer | Ben Kimock <kimockb@gmail.com> | 2022-07-16 12:26:37 -0400 |
| commit | c9373903e759c96f2ab8ba2a78c6799b5d92b105 (patch) | |
| tree | 3e7771c5c55f87e448cac469c225700d2811da54 | |
| parent | 9fb32dc924653e35950f17c8d91793c9ca983d03 (diff) | |
| download | rust-c9373903e759c96f2ab8ba2a78c6799b5d92b105.tar.gz rust-c9373903e759c96f2ab8ba2a78c6799b5d92b105.zip | |
Rearrange slice::split_mut to remove bounds check
| -rw-r--r-- | library/core/src/slice/iter.rs | 19 |
1 files changed, 9 insertions, 10 deletions
diff --git a/library/core/src/slice/iter.rs b/library/core/src/slice/iter.rs index 35d00b9dda6..a1889e3d21c 100644 --- a/library/core/src/slice/iter.rs +++ b/library/core/src/slice/iter.rs @@ -710,18 +710,17 @@ where return None; } - let idx_opt = { - // work around borrowck limitations - let pred = &mut self.pred; - self.v.iter().position(|x| (*pred)(x)) - }; - match idx_opt { + match self.v.iter().position(|x| (self.pred)(x)) { None => self.finish(), Some(idx) => { - let tmp = mem::replace(&mut self.v, &mut []); - let (head, tail) = tmp.split_at_mut(idx); - self.v = &mut tail[1..]; - Some(head) + let tmp = mem::take(&mut self.v); + // idx is the index of the element we are splitting on. We want to set self to the + // region after idx, and return the subslice before and not including idx. + // So first we split after idx + let (head, tail) = tmp.split_at_mut(idx + 1); + self.v = tail; + // Then return the subslice up to but not including the found element + Some(&mut head[..idx]) } } } |
