1 /// Used for indexing operations (`container[index]`) in immutable contexts.
3 /// `container[index]` is actually syntactic sugar for `*container.index(index)`,
4 /// but only when used as an immutable value. If a mutable value is requested,
5 /// [`IndexMut`] is used instead. This allows nice things such as
6 /// `let value = v[index]` if the type of `value` implements [`Copy`].
8 /// [`IndexMut`]: ../../std/ops/trait.IndexMut.html
9 /// [`Copy`]: ../../std/marker/trait.Copy.html
13 /// The following example implements `Index` on a read-only `NucleotideCount`
14 /// container, enabling individual counts to be retrieved with index syntax.
17 /// use std::ops::Index;
26 /// struct NucleotideCount {
33 /// impl Index<Nucleotide> for NucleotideCount {
34 /// type Output = usize;
36 /// fn index(&self, nucleotide: Nucleotide) -> &Self::Output {
37 /// match nucleotide {
38 /// Nucleotide::A => &self.a,
39 /// Nucleotide::C => &self.c,
40 /// Nucleotide::G => &self.g,
41 /// Nucleotide::T => &self.t,
46 /// let nucleotide_count = NucleotideCount {a: 14, c: 9, g: 10, t: 12};
47 /// assert_eq!(nucleotide_count[Nucleotide::A], 14);
48 /// assert_eq!(nucleotide_count[Nucleotide::C], 9);
49 /// assert_eq!(nucleotide_count[Nucleotide::G], 10);
50 /// assert_eq!(nucleotide_count[Nucleotide::T], 12);
53 #[rustc_on_unimplemented(
54 message = "the type `{Self}` cannot be indexed by `{Idx}`",
55 label = "`{Self}` cannot be indexed by `{Idx}`"
57 #[stable(feature = "rust1", since = "1.0.0")]
61 pub trait Index<Idx: ?Sized> {
62 /// The returned type after indexing.
63 #[stable(feature = "rust1", since = "1.0.0")]
66 /// Performs the indexing (`container[index]`) operation.
67 #[stable(feature = "rust1", since = "1.0.0")]
68 fn index(&self, index: Idx) -> &Self::Output;
71 /// Used for indexing operations (`container[index]`) in mutable contexts.
73 /// `container[index]` is actually syntactic sugar for
74 /// `*container.index_mut(index)`, but only when used as a mutable value. If
75 /// an immutable value is requested, the [`Index`] trait is used instead. This
76 /// allows nice things such as `v[index] = value`.
78 /// [`Index`]: ../../std/ops/trait.Index.html
82 /// A very simple implementation of a `Balance` struct that has two sides, where
83 /// each can be indexed mutably and immutably.
86 /// use std::ops::{Index,IndexMut};
94 /// #[derive(Debug, PartialEq)]
101 /// pub left: Weight,
102 /// pub right: Weight,
105 /// impl Index<Side> for Balance {
106 /// type Output = Weight;
108 /// fn index(&self, index: Side) -> &Self::Output {
109 /// println!("Accessing {:?}-side of balance immutably", index);
111 /// Side::Left => &self.left,
112 /// Side::Right => &self.right,
117 /// impl IndexMut<Side> for Balance {
118 /// fn index_mut(&mut self, index: Side) -> &mut Self::Output {
119 /// println!("Accessing {:?}-side of balance mutably", index);
121 /// Side::Left => &mut self.left,
122 /// Side::Right => &mut self.right,
127 /// let mut balance = Balance {
128 /// right: Weight::Kilogram(2.5),
129 /// left: Weight::Pound(1.5),
132 /// // In this case, `balance[Side::Right]` is sugar for
133 /// // `*balance.index(Side::Right)`, since we are only *reading*
134 /// // `balance[Side::Right]`, not writing it.
135 /// assert_eq!(balance[Side::Right], Weight::Kilogram(2.5));
137 /// // However, in this case `balance[Side::Left]` is sugar for
138 /// // `*balance.index_mut(Side::Left)`, since we are writing
139 /// // `balance[Side::Left]`.
140 /// balance[Side::Left] = Weight::Kilogram(3.0);
142 #[lang = "index_mut"]
143 #[rustc_on_unimplemented(
146 note = "you can use `.chars().nth()` or `.bytes().nth()`
147 see chapter in The Book <https://doc.rust-lang.org/book/ch08-02-strings.html#indexing-into-strings>"
151 note = "you can use `.chars().nth()` or `.bytes().nth()`
152 see chapter in The Book <https://doc.rust-lang.org/book/ch08-02-strings.html#indexing-into-strings>"
155 _Self = "std::string::String",
156 note = "you can use `.chars().nth()` or `.bytes().nth()`
157 see chapter in The Book <https://doc.rust-lang.org/book/ch08-02-strings.html#indexing-into-strings>"
159 message = "the type `{Self}` cannot be mutably indexed by `{Idx}`",
160 label = "`{Self}` cannot be mutably indexed by `{Idx}`"
162 #[stable(feature = "rust1", since = "1.0.0")]
166 pub trait IndexMut<Idx: ?Sized>: Index<Idx> {
167 /// Performs the mutable indexing (`container[index]`) operation.
168 #[stable(feature = "rust1", since = "1.0.0")]
169 fn index_mut(&mut self, index: Idx) -> &mut Self::Output;