Trait At

Source
pub trait At<T> {
    // Required methods
    fn at(&self, index: impl IntoIndex) -> &T;
    fn try_at(&self, index: impl IntoIndex) -> Option<&T>;
}
Expand description

Trait to get a reference to an element at a given index.

The index can be of any type that implements IntoIndex.

Note that “out of bounds” below applies to indices that either don’t exist or where the impl IntoIndex fails because the value can not be converted to usize.

Required Methods§

Source

fn at(&self, index: impl IntoIndex) -> &T

Returns a reference to the element at the given index.

§Panics

Panics when the index is out of bounds.

§Example
use into_index::At;
let v = vec![1,2,3];

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

fn try_at(&self, index: impl IntoIndex) -> Option<&T>

Returns Some(&T) to the element at the given index.

Returns None when the index is out of bounds.

§Example
use into_index::At;
let v = vec![1,2,3];

assert_eq!(*v.try_at(1u8).unwrap(), 2);
assert_eq!(v.try_at(10u8), None);

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<T> At<T> for Vec<T>

At is implemented for any Vec<T>.

Source§

fn at(&self, index: impl IntoIndex) -> &T

Source§

fn try_at(&self, index: impl IntoIndex) -> Option<&T>

Implementors§

Source§

impl<T: 'static> At<T> for UniVec