pub trait SliceExt<I, O>where
O: ?Sized,{
// Required method
fn eget(&self, index: I) -> Result<&O>;
}Expand description
Extension trait for slices.
Required Methods§
Sourcefn eget(&self, index: I) -> Result<&O>
fn eget(&self, index: I) -> Result<&O>
Returns a reference to an element or subslice depending on the type of index.
- If given a position, returns a reference to the element at that
position or [
Err(_)] if out of bounds. - If given a range, returns the subslice corresponding to that range,
or [
Err(_)] if out of bounds.
§Examples
let v = [10, 40, 30];
assert_eq!(Ok(&40), v.eget(1));
assert_eq!(Ok(&[10, 40][..]), v.eget(0..2));
assert_eq!(&ErrorType::IndexOutOfBounds(3, 3), v.eget(3).err().unwrap().ty());
assert_eq!(&ErrorType::RangeOutOfBounds(0, 4, 3), v.eget(0..4).err().unwrap().ty());