diff options
| author | Mazdak Farrokhzad <twingoow@gmail.com> | 2019-06-20 08:36:00 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-06-20 08:36:00 +0200 |
| commit | 2e7e131b8e4ee2addf7f0ae64108a4da8210b369 (patch) | |
| tree | 8dcbda7e14fc1eaaef6a1593c9b4673405d9864e /src/libcore/tests | |
| parent | 3e08f1b57ea6df87ee2d01f31339958998f8ea26 (diff) | |
| parent | 97a6c932e02fcd7f55fdad9aef76c5619f91f481 (diff) | |
| download | rust-2e7e131b8e4ee2addf7f0ae64108a4da8210b369.tar.gz rust-2e7e131b8e4ee2addf7f0ae64108a4da8210b369.zip | |
Rollup merge of #60772 - timvermeulen:slice_iter_nth_back, r=scottmcm
Implement nth_back for slice::{Iter, IterMut}
Part of #54054.
I implemented `nth_back` as straightforwardly as I could, and then slightly changed `nth` to match `nth_back`. I believe I did so correctly, but please double-check 🙂
I also added the helper methods `zst_shrink`, `next_unchecked`, and `next_back_unchecked` to get rid of some duplicated code. These changes hopefully make this code easier to understand for new contributors like me.
I noticed the `is_empty!` and `len!` macros which sole purpose seems to be inlining, according to the comment right above them, but the `is_empty` and `len` methods are already marked with `#[inline(always)]`. Does that mean we could replace these macros with method calls, without affecting anything? I'd love to get rid of them.
Diffstat (limited to 'src/libcore/tests')
| -rw-r--r-- | src/libcore/tests/slice.rs | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/src/libcore/tests/slice.rs b/src/libcore/tests/slice.rs index eaa799fa96e..03e65d2fe0b 100644 --- a/src/libcore/tests/slice.rs +++ b/src/libcore/tests/slice.rs @@ -89,6 +89,19 @@ fn test_iterator_nth() { } #[test] +fn test_iterator_nth_back() { + let v: &[_] = &[0, 1, 2, 3, 4]; + for i in 0..v.len() { + assert_eq!(v.iter().nth_back(i).unwrap(), &v[v.len() - i - 1]); + } + assert_eq!(v.iter().nth_back(v.len()), None); + + let mut iter = v.iter(); + assert_eq!(iter.nth_back(2).unwrap(), &v[2]); + assert_eq!(iter.nth_back(1).unwrap(), &v[0]); +} + +#[test] fn test_iterator_last() { let v: &[_] = &[0, 1, 2, 3, 4]; assert_eq!(v.iter().last().unwrap(), &4); |
