#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

#2680 in Rust patterns

Download history 114/week @ 2024-03-24 312/week @ 2024-03-31 144/week @ 2024-04-07 438/week @ 2024-04-14 119/week @ 2024-04-21 90/week @ 2024-04-28 172/week @ 2024-05-05 166/week @ 2024-05-12 184/week @ 2024-05-19 142/week @ 2024-05-26 590/week @ 2024-06-02 137/week @ 2024-06-09 174/week @ 2024-06-16 515/week @ 2024-06-23 297/week @ 2024-06-30 323/week @ 2024-07-07

1,312 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