6 releases
| 0.1.5 | Oct 23, 2018 |
|---|---|
| 0.1.4 | Oct 22, 2018 |
| 0.1.3 | Aug 5, 2018 |
#197 in Rust patterns
15 downloads per month
8KB
166 lines
rev_slice
A simple alternative to negative indexing on rust slices
Ever wanted a[-1] in Rust? With this crate, you can do that as a.rev()[0].
Also provided are the other slice methods on the reversed slice, and you can
.rev() again to get the original slice back. So you can, for example,
remove the last element of a slice with a.rev()[1..].rev().
lib.rs:
Offers a reversed view into a slice.
To use, import the SliceExt trait to get the .rev() and .rev_mut
extension methods on slices. Then treat the returned RevSlice like
you would an ordinary slice: index it, split it, iterate it, whatever.
Example:
extern crate rev_slice;
use rev_slice::SliceExt;
let r = [1, 2, 4, 9, 16, 25].rev();
assert_eq!(r[0], 25);
assert_eq!(r[1..3].rev(), &[9, 16]);
assert_eq!(r.split_first().unwrap().0, &25);
let mut it = r.iter().cloned().skip(2);
assert_eq!(it.next(), Some(9));
assert_eq!(it.next(), Some(4));
assert_eq!(it.next(), Some(2));