Trait sp_std::ops::Index

1.0.0 · source ·
pub trait Index<Idx>where
    Idx: ?Sized,{
    type Output: ?Sized;

    // Required method
    fn index(&self, index: Idx) -> &Self::Output;
}
Expand description

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);

Required Associated Types§

source

type Output: ?Sized

The returned type after indexing.

Required Methods§

source

fn index(&self, index: Idx) -> &Self::Output

Performs the indexing (container[index]) operation.

Panics

May panic if the index is out of bounds.

Implementors§

source§

impl Index<Range<usize>> for String

§

type Output = str

source§

impl Index<RangeFrom<usize>> for String

§

type Output = str

1.47.0 · source§

impl Index<RangeFrom<usize>> for CStr

§

type Output = CStr

1.7.0 · source§

impl Index<RangeFull> for CString

§

type Output = CStr

source§

impl Index<RangeFull> for String

§

type Output = str

source§

impl Index<RangeFull> for OsString

1.26.0 · source§

impl Index<RangeInclusive<usize>> for String

§

type Output = str

source§

impl Index<RangeTo<usize>> for String

§

type Output = str

1.26.0 · source§

impl Index<RangeToInclusive<usize>> for String

§

type Output = str

source§

impl<I> Index<I> for strwhere I: SliceIndex<str>,

§

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

source§

impl<I, T, const LANES: usize> Index<I> for Simd<T, LANES>where T: SimdElement, LaneCount<LANES>: SupportedLaneCount, I: SliceIndex<[T]>,

§

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

source§

impl<K, Q, V, A> Index<&Q> for BTreeMap<K, V, A>where A: Allocator + Clone, K: Borrow<Q> + Ord, Q: Ord + ?Sized,

§

type Output = V

source§

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

§

type Output = V

source§

impl<T, A> Index<usize> for VecDeque<T, A>where A: Allocator,

§

type Output = T

source§

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

§

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

source§

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

§

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

1.50.0 · source§

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

§

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

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

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

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

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

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

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

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

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

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

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

impl<A, O, Idx> Index<Idx> for BitArray<A, O>where A: BitViewSized, O: BitOrder, BitSlice<A::Store, O>: Index<Idx>,

impl Index<Range<usize>> for BStr

impl Index<RangeFull> for BStr

impl Index<RangeTo<usize>> for BStr

impl Index<usize> for BStr

impl Index<&Id> for Command

impl<K, V> Index<K> for PrimaryMap<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 BoxedSlice<K, V>where K: EntityRef,

impl Index<usize> for Scalar

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

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

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

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

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

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

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

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

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

impl<T: Scalar, const D: usize> Index<(usize, usize)> for Rotation<T, D>

impl<T: Scalar, const D: usize> Index<usize> for Point<T, D>

impl<T: Scalar> Index<usize> for Quaternion<T>

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

impl<T: RealField, C: TCategory, const D: usize> Index<(usize, usize)> for Transform<T, C, D>where Const<D>: DimNameAdd<U1>, DefaultAllocator: Allocator<T, DimNameSum<Const<D>, U1>, DimNameSum<Const<D>, U1>>,

impl<N, E, Ty: EdgeType, Null: Nullable<Wrapped = E>, Ix: IndexType> Index<(NodeIndex<Ix>, NodeIndex<Ix>)> for MatrixGraph<N, E, Ty, Null, Ix>

impl<N, E, Ty, Ix> Index<Ix> for Csr<N, E, Ty, Ix>where Ty: EdgeType, Ix: IndexType,

impl<N, E, Ty, Ix> Index<NodeIndex<Ix>> for Graph<N, E, Ty, Ix>where Ty: EdgeType, Ix: IndexType,

impl<N, E, Ty, Ix> Index<NodeIndex<Ix>> for StableGraph<N, E, Ty, Ix>where Ty: EdgeType, Ix: IndexType,

impl<N, E, Ty, Ix> Index<EdgeIndex<Ix>> for StableGraph<N, E, Ty, Ix>where Ty: EdgeType, Ix: IndexType,

impl<'a, G, I> Index<I> for Frozen<'a, G>where G: Index<I>,

impl<N, E, Ty: EdgeType, Null: Nullable<Wrapped = E>, Ix: IndexType> Index<NodeIndex<Ix>> for MatrixGraph<N, E, Ty, Null, Ix>

impl<N, E, Ty> Index<(N, N)> for GraphMap<N, E, Ty>where N: NodeTrait, Ty: EdgeType,

impl<N, E, Ty, Ix> Index<EdgeIndex<Ix>> for Graph<N, E, Ty, Ix>where Ty: EdgeType, Ix: IndexType,

impl<T> Index<PeerSet> for PerPeerSet<T>

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

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

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

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

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

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

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 Index<usize> for Message

impl Index<usize> for KeyPair

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

impl<I> Index<I> for Valuewhere I: Index,

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

impl<K, V, S> Index<K> for SparseSecondaryMap<K, V, S>where K: Key, S: BuildHasher,

impl<K: Key, V> Index<K> for DenseSlotMap<K, V>

impl<K: Key, V> Index<K> for HopSlotMap<K, V>

impl<K: Key, V> Index<K> for SecondaryMap<K, V>

impl<K: Key, V> Index<K> for SlotMap<K, V>

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

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

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

impl<D: AsRef<[f64]>> Index<usize> for Data<D>

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

impl<T: MibArg> Index<usize> for MibStr<T>

impl<T: MibArg> Index<usize> for Mib<T>

impl<A: Array, I: SliceIndex<[A::Item]>> Index<I> for TinyVec<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 ArrayVec<A>

impl<I> Index<I> for Valuewhere I: Index,

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

impl<I> Index<I> for Itemwhere I: Index,

impl<'s> Index<&'s str> for Table

impl<'s> Index<&'s str> for InlineTable

impl<'s> Index<&'s str> for Document

impl Index<RangeFull> for Url

impl Index<Range<Position>> for Url

impl<I, T> Index<I> for StoreContextMut<'_, T>where StoreData: Index<I>,

impl<I, T> Index<I> for StoreContext<'_, T>where StoreData: Index<I>,

impl<T> Index<T> for ModuleTypesBuilderwhere ModuleTypes: Index<T>,