Trait sp_std::ops::Index1.0.0[][src]

#[lang = "index"]pub trait Index<Idx> where
    Idx: ?Sized
{ type Output: ?Sized; pub fn index(&self, index: Idx) -> &Self::Output; }
[]

Used for indexing operations (container[index]) in immutable contexts.

container[index] is actually syntactic sugar for *container.index(index), but only when used as an immutable value. If a mutable value is requested, IndexMut is used instead. This allows nice things such as let value = v[index] if the type of value implements Copy.

Examples

The following example implements Index on a read-only NucleotideCount container, enabling individual counts to be retrieved with index syntax.

use std::ops::Index;

enum Nucleotide {
    A,
    C,
    G,
    T,
}

struct NucleotideCount {
    a: usize,
    c: usize,
    g: usize,
    t: usize,
}

impl Index<Nucleotide> for NucleotideCount {
    type Output = usize;

    fn index(&self, nucleotide: Nucleotide) -> &Self::Output {
        match nucleotide {
            Nucleotide::A => &self.a,
            Nucleotide::C => &self.c,
            Nucleotide::G => &self.g,
            Nucleotide::T => &self.t,
        }
    }
}

let nucleotide_count = NucleotideCount {a: 14, c: 9, g: 10, t: 12};
assert_eq!(nucleotide_count[Nucleotide::A], 14);
assert_eq!(nucleotide_count[Nucleotide::C], 9);
assert_eq!(nucleotide_count[Nucleotide::G], 10);
assert_eq!(nucleotide_count[Nucleotide::T], 12);

Associated Types

type Output: ?Sized[src][]

The returned type after indexing.

Required methods

pub fn index(&self, index: Idx) -> &Self::Output[src][]

Performs the indexing (container[index]) operation.

Implementations on Foreign Types

impl<'_, K, Q, V, S> Index<&'_ Q> for HashMap<K, V, S> where
    K: Eq + Hash + Borrow<Q>,
    S: BuildHasher,
    Q: Eq + Hash + ?Sized
[src][]

type Output = V

pub fn index(&self, key: &Q) -> &V

Notable traits for &'_ mut I

impl<'_, I> Iterator for &'_ mut I where
    I: Iterator + ?Sized
type Item = <I as Iterator>::Item;
[src][]

Returns a reference to the value corresponding to the supplied key.

Panics

Panics if the key is not present in the HashMap.

impl Index<RangeFull> for CString[src][]

type Output = CStr

impl Index<RangeFrom<usize>> for CStr[src][]

type Output = CStr

impl Index<RangeFull> for OsString[src][]

type Output = OsStr

impl<T, I, const N: usize> Index<I> for [T; N] where
    [T]: Index<I>, 
[src][]

type Output = <[T] as Index<I>>::Output

impl<I> Index<I> for str where
    I: SliceIndex<str>, 
[src][]

type Output = <I as SliceIndex<str>>::Output

impl<T, I> Index<I> for [T] where
    I: SliceIndex<[T]>, 
[src][]

type Output = <I as SliceIndex<[T]>>::Output

impl Index<RangeTo<usize>> for String[src][]

type Output = str

impl Index<RangeFull> for String[src][]

type Output = str

impl Index<RangeToInclusive<usize>> for String[src][]

type Output = str

impl Index<RangeFrom<usize>> for String[src][]

type Output = str

impl Index<RangeInclusive<usize>> for String[src][]

type Output = str

impl Index<Range<usize>> for String[src][]

type Output = str

Implementors

impl<'_, K, Q, V> Index<&'_ Q> for BTreeMap<K, V> where
    K: Borrow<Q> + Ord,
    Q: Ord + ?Sized
[src][+]

type Output = V

pub fn index(&self, key: &Q) -> &V

Notable traits for &'_ mut I

impl<'_, I> Iterator for &'_ mut I where
    I: Iterator + ?Sized
type Item = <I as Iterator>::Item;
[src][]

Returns a reference to the value corresponding to the supplied key.

Panics

Panics if the key is not present in the BTreeMap.

impl<A> Index<usize> for VecDeque<A>[src][+]

type Output = A

impl<T, I, A> Index<I> for Vec<T, A> where
    A: Allocator,
    I: SliceIndex<[T]>, 
[src][+]

type Output = <I as SliceIndex<[T]>>::Output

impl<O, V, Idx> Index<Idx> for BitArray<O, V> where
    O: BitOrder,
    V: BitView,
    BitSlice<O, V::Store>: Index<Idx>, 

impl<O, T> Index<usize> for BitSlice<O, T> where
    O: BitOrder,
    T: BitStore

impl<O, T> Index<Range<usize>> for BitSlice<O, T> where
    O: BitOrder,
    T: BitStore

impl<O, T> Index<RangeFrom<usize>> for BitSlice<O, T> where
    O: BitOrder,
    T: BitStore

impl<O, T> Index<RangeFull> for BitSlice<O, T> where
    O: BitOrder,
    T: BitStore

impl<O, T> Index<RangeInclusive<usize>> for BitSlice<O, T> where
    O: BitOrder,
    T: BitStore

impl<O, T> Index<RangeTo<usize>> for BitSlice<O, T> where
    O: BitOrder,
    T: BitStore

impl<O, T> Index<RangeToInclusive<usize>> for BitSlice<O, T> where
    O: BitOrder,
    T: BitStore

impl<O, T, Idx> Index<Idx> for BitBox<O, T> where
    O: BitOrder,
    T: BitStore,
    BitSlice<O, T>: Index<Idx>, 

impl<O, T, Idx> Index<Idx> for BitVec<O, T> where
    O: BitOrder,
    T: BitStore,
    BitSlice<O, T>: Index<Idx>, 

impl Index<usize> for BStr

impl Index<RangeFull> for BStr

impl Index<Range<usize>> for BStr

impl Index<RangeInclusive<usize>> for BStr

impl Index<RangeFrom<usize>> for BStr

impl Index<RangeTo<usize>> for BStr

impl Index<RangeToInclusive<usize>> for BStr

impl Index<Range<usize>> for UninitSlice

impl Index<RangeFrom<usize>> for UninitSlice

impl Index<RangeFull> for UninitSlice

impl Index<RangeInclusive<usize>> for UninitSlice

impl Index<RangeTo<usize>> for UninitSlice

impl Index<RangeToInclusive<usize>> for UninitSlice

impl Index<Inst> for DataFlowGraph

impl Index<StackSlot> for StackSlots

impl<K, V> Index<K> for BoxedSlice<K, V> where
    K: EntityRef

impl<K, V> Index<K> for SecondaryMap<K, V> where
    K: EntityRef,
    V: Clone

impl<K, V> Index<K> for PrimaryMap<K, V> where
    K: EntityRef

impl Index<usize> for Scalar

impl<'input, Endian> Index<usize> for EndianSlice<'input, Endian> where
    Endian: Endianity

impl<'input, Endian> Index<RangeFrom<usize>> for EndianSlice<'input, Endian> where
    Endian: Endianity

impl<Endian, T> Index<usize> for EndianReader<Endian, T> where
    Endian: Endianity,
    T: CloneStableDeref<Target = [u8]> + Debug

impl<Endian, T> Index<RangeFrom<usize>> for EndianReader<Endian, T> where
    Endian: Endianity,
    T: CloneStableDeref<Target = [u8]> + Debug

impl<K, Q: ?Sized, V, S> Index<&'_ Q> for HashMap<K, V, S> where
    K: Eq + Hash + Borrow<Q>,
    Q: Eq + Hash,
    S: BuildHasher

impl<'a, K, T> Index<K> for HeaderMap<T> where
    K: AsHeaderName

impl<K, V, Q: ?Sized, S> Index<&'_ Q> for IndexMap<K, V, S> where
    Q: Hash + Equivalent<K>,
    K: Hash + Eq,
    S: BuildHasher

impl<K, V, S> Index<usize> for IndexMap<K, V, S>

impl<T, S> Index<usize> for IndexSet<T, S>

impl<'a, K, V, S, Q: ?Sized> Index<&'a Q> for LinkedHashMap<K, V, S> where
    K: Hash + Eq + Borrow<Q>,
    S: BuildHasher,
    Q: Eq + Hash

impl<N: Scalar, R: Dim, C: Dim, S: Storage<N, R, C>> Index<usize> for Matrix<N, R, C, S>

impl<N, R: Dim, C: Dim, S> Index<(usize, usize)> for Matrix<N, R, C, S> where
    N: Scalar,
    S: Storage<N, R, C>, 

impl<N: Scalar, D: DimName> Index<usize> for Point<N, D> where
    DefaultAllocator: Allocator<N, D>, 

impl<N: Scalar, D: DimName> Index<(usize, usize)> for Rotation<N, D> where
    DefaultAllocator: Allocator<N, D, D>, 

impl<N: SimdRealField> Index<usize> for Quaternion<N>

impl<N: RealField, D, C: TCategory> Index<(usize, usize)> for Transform<N, D, C> where
    D: DimName + DimNameAdd<U1>,
    DefaultAllocator: Allocator<N, DimNameSum<D, U1>, DimNameSum<D, U1>>, 

impl<I> Index<I> for H128 where
    I: SliceIndex<[u8]>, 

impl<I> Index<I> for H160 where
    I: SliceIndex<[u8]>, 

impl<I> Index<I> for H256 where
    I: SliceIndex<[u8]>, 

impl<I> Index<I> for H512 where
    I: SliceIndex<[u8]>, 

impl<TyIx, Ty> Index<TyIx> for TypedIxVec<TyIx, Ty> where
    TyIx: Into<u32>, 

impl<'t> Index<usize> for Captures<'t>

impl<'t, 'i> Index<&'i str> for Captures<'t>

impl<'t> Index<usize> for Captures<'t>

impl<'t, 'i> Index<&'i str> for Captures<'t>

impl<'a, Q: ?Sized> Index<&'a Q> for Map<String, Value> where
    String: Borrow<Q>,
    Q: Ord + Eq + Hash

impl<I> Index<I> for Value where
    I: Index

impl<T> Index<usize> for Slab<T>

impl<A: Array, I: SliceIndex<[A::Item]>> Index<I> for SmallVec<A>

impl<T, P> Index<usize> for Punctuated<T, P>

impl<A: Array, I: SliceIndex<[A::Item]>> Index<I> for ArrayVec<A>

impl<'s, T, I> Index<I> for SliceVec<'s, T> where
    I: SliceIndex<[T]>, 

impl<A: Array, I: SliceIndex<[A::Item]>> Index<I> for TinyVec<A>

impl<'a, Q: ?Sized> Index<&'a Q> for Map<String, Value> where
    String: Borrow<Q>,
    Q: Ord + Eq + Hash

impl<I> Index<I> for Value where
    I: Index

impl Index<RangeFull> for Url

impl Index<RangeFrom<Position>> for Url

impl Index<RangeTo<Position>> for Url

impl Index<Range<Position>> for Url

impl<T> Index<usize> for Arena<T>

impl<V> Index<usize> for VecMap<V>

impl<'a, V> Index<&'a usize> for VecMap<V>