#indexing #traits #vec

into_index

Helper traits allowing indexing into vectors and similar types by other types than usize

1 unstable release

0.1.0 Apr 30, 2024

#2339 in Rust patterns


Used in univec

MIT/Apache

8KB

IntoIndex

Sometimes it is more convenient to use another type than usize to calculate and store indices. This crate provides an IntoIndex trait that can be used to convert any type into an usize. For types that implement TryInto<usize> this is already implented. Naturally some of these conversions are fallible,​ the into_index() method may panic in this case. A try_into_index() method that returns a Result for handling such errors are available as well.

At and AtMut

Further the At and AtMut traits use IntoIndex for providing indexing without the associated Output type that is required for the Index and IndexMut traits. A implementation for std Vec<T> is already provided.

Example

use into_index::{IntoIndex, At};
let v = vec![1,2,3];

assert_eq!(*v.at(1u8), 2);

No runtime deps