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`].
10 /// The following example implements `Index` on a read-only `NucleotideCount`
11 /// container, enabling individual counts to be retrieved with index syntax.
14 /// use std::ops::Index;
23 /// struct NucleotideCount {
30 /// impl Index<Nucleotide> for NucleotideCount {
31 /// type Output = usize;
33 /// fn index(&self, nucleotide: Nucleotide) -> &Self::Output {
34 /// match nucleotide {
35 /// Nucleotide::A => &self.a,
36 /// Nucleotide::C => &self.c,
37 /// Nucleotide::G => &self.g,
38 /// Nucleotide::T => &self.t,
43 /// let nucleotide_count = NucleotideCount {a: 14, c: 9, g: 10, t: 12};
44 /// assert_eq!(nucleotide_count[Nucleotide::A], 14);
45 /// assert_eq!(nucleotide_count[Nucleotide::C], 9);
46 /// assert_eq!(nucleotide_count[Nucleotide::G], 10);
47 /// assert_eq!(nucleotide_count[Nucleotide::T], 12);
50 #[rustc_on_unimplemented(
51 message = "the type `{Self}` cannot be indexed by `{Idx}`",
52 label = "`{Self}` cannot be indexed by `{Idx}`"
54 #[stable(feature = "rust1", since = "1.0.0")]
59 pub trait Index<Idx: ?Sized> {
60 /// The returned type after indexing.
61 #[stable(feature = "rust1", since = "1.0.0")]
64 /// Performs the indexing (`container[index]`) operation.
68 /// May panic if the index is out of bounds.
69 #[stable(feature = "rust1", since = "1.0.0")]
71 fn index(&self, index: Idx) -> &Self::Output;
74 /// Used for indexing operations (`container[index]`) in mutable contexts.
76 /// `container[index]` is actually syntactic sugar for
77 /// `*container.index_mut(index)`, but only when used as a mutable value. If
78 /// an immutable value is requested, the [`Index`] trait is used instead. This
79 /// allows nice things such as `v[index] = value`.
83 /// A very simple implementation of a `Balance` struct that has two sides, where
84 /// each can be indexed mutably and immutably.
87 /// use std::ops::{Index, IndexMut};
95 /// #[derive(Debug, PartialEq)]
102 /// pub left: Weight,
103 /// pub right: Weight,
106 /// impl Index<Side> for Balance {
107 /// type Output = Weight;
109 /// fn index(&self, index: Side) -> &Self::Output {
110 /// println!("Accessing {index:?}-side of balance immutably");
112 /// Side::Left => &self.left,
113 /// Side::Right => &self.right,
118 /// impl IndexMut<Side> for Balance {
119 /// fn index_mut(&mut self, index: Side) -> &mut Self::Output {
120 /// println!("Accessing {index:?}-side of balance mutably");
122 /// Side::Left => &mut self.left,
123 /// Side::Right => &mut self.right,
128 /// let mut balance = Balance {
129 /// right: Weight::Kilogram(2.5),
130 /// left: Weight::Pound(1.5),
133 /// // In this case, `balance[Side::Right]` is sugar for
134 /// // `*balance.index(Side::Right)`, since we are only *reading*
135 /// // `balance[Side::Right]`, not writing it.
136 /// assert_eq!(balance[Side::Right], Weight::Kilogram(2.5));
138 /// // However, in this case `balance[Side::Left]` is sugar for
139 /// // `*balance.index_mut(Side::Left)`, since we are writing
140 /// // `balance[Side::Left]`.
141 /// balance[Side::Left] = Weight::Kilogram(3.0);
143 #[lang = "index_mut"]
144 #[rustc_on_unimplemented(
147 note = "you can use `.chars().nth()` or `.bytes().nth()`
148 see chapter in The Book <https://doc.rust-lang.org/book/ch08-02-strings.html#indexing-into-strings>"
152 note = "you can use `.chars().nth()` or `.bytes().nth()`
153 see chapter in The Book <https://doc.rust-lang.org/book/ch08-02-strings.html#indexing-into-strings>"
156 _Self = "std::string::String",
157 note = "you can use `.chars().nth()` or `.bytes().nth()`
158 see chapter in The Book <https://doc.rust-lang.org/book/ch08-02-strings.html#indexing-into-strings>"
160 message = "the type `{Self}` cannot be mutably indexed by `{Idx}`",
161 label = "`{Self}` cannot be mutably indexed by `{Idx}`"
163 #[stable(feature = "rust1", since = "1.0.0")]
168 pub trait IndexMut<Idx: ?Sized>: Index<Idx> {
169 /// Performs the mutable indexing (`container[index]`) operation.
173 /// May panic if the index is out of bounds.
174 #[stable(feature = "rust1", since = "1.0.0")]
176 fn index_mut(&mut self, index: Idx) -> &mut Self::Output;