#reverse #indexing

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

#1426 in Rust patterns

Download history 185/week @ 2023-10-16 194/week @ 2023-10-23 201/week @ 2023-10-30 322/week @ 2023-11-06 254/week @ 2023-11-13 524/week @ 2023-11-20 516/week @ 2023-11-27 243/week @ 2023-12-04 402/week @ 2023-12-11 341/week @ 2023-12-18 161/week @ 2023-12-25 331/week @ 2024-01-01 363/week @ 2024-01-08 142/week @ 2024-01-15 504/week @ 2024-01-22 540/week @ 2024-01-29

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