Trait nom::lib::std::ops::Index1.0.0[][src]

pub trait Index<Idx> where
    Idx: ?Sized
{ type Output: ?Sized; 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);

Associated Types

The returned type after indexing.

Required methods

Performs the indexing (container[index]) operation.

Panics

May panic if the index is out of bounds.

Implementations on Foreign Types

Implementors

impl<'a> Index<&'a PackageId> for Metadata

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<K, Q: ?Sized, V, S, A> Index<&'_ Q> for HashMap<K, V, S, A> where
    K: Eq + Hash + Borrow<Q>,
    Q: Eq + Hash,
    S: BuildHasher,
    A: Allocator + Clone

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<T: Collection> Index<usize> for Nested<T>

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<EdgeIndex<Ix>> for Graph<N, E, Ty, Ix> where
    Ty: EdgeType,
    Ix: IndexType

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

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> Index<&'a Q> for Map<String, Value> where
    String: Borrow<Q>,
    Q: ?Sized + Ord + Eq + Hash

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

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

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

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