| Crates.io | at |
| lib.rs | at |
| version | 0.2.0 |
| created_at | 2018-11-27 04:43:40.133859+00 |
| updated_at | 2025-07-01 22:25:58.757985+00 |
| description | Helpers for indexing slices. |
| homepage | |
| repository | https://github.com/abgros/at |
| max_upload_size | |
| id | 98865 |
| size | 26,525 |
Various utility functions for indexing slices.
This crate provides three methods for indexing slices: at, ref_at, and mut_at.
These methods offer a few benefits over standard indexing:
usize1nums.at(-1) returns the last element2unsafe-unchecked feature;
this is not recommended unless you absolutely need the performance gainsAll this happens with zero runtime overhead compared to standard indexing.
However, note that checking the validity of signed types is slightly more complex
than for a usize due to negative indexing. Signed indexing does not incur any
overhead when the index is known at compile time.
use at::At;
let mut v = vec![8, 2, 1, 0];
assert_eq!(v.at(-1), 0);
assert_eq!(v.ref_at(2), &1);
assert_eq!(v.mut_at(-3), &mut 2);
Specifically, the trait bound is TryInto<isize> + TryInto<usize> + Debug + Copy. ↩
Negative indices are converted into an isize, so they cannot be smaller than isize::MIN.
Therefore, [(); usize::MAX].at(-(usize::MAX as i128)) will panic, even though you might
expect it to successfully return the first element of the slice. ↩