#indexing #slice #reverse #view #negative #reversed #rev

no-std rev_slice

A newtype for operating on a reversed view of a slice

5 releases

Uses old Rust 2015

0.1.5 Oct 23, 2018
0.1.4 Oct 22, 2018
0.1.3 Aug 5, 2018

#2761 in Rust patterns

Download history 149/week @ 2024-07-15 234/week @ 2024-07-22 356/week @ 2024-07-29 266/week @ 2024-08-05 358/week @ 2024-08-12 39/week @ 2024-08-19 64/week @ 2024-08-26 108/week @ 2024-09-02 79/week @ 2024-09-09 23/week @ 2024-09-16 36/week @ 2024-09-23 31/week @ 2024-09-30 37/week @ 2024-10-07 15/week @ 2024-10-14 7/week @ 2024-10-28

68 downloads per month

MIT license

9KB
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));

No runtime deps

Features