1 # What `#[derive(Index)]` generates
3 Deriving `Index` only works for a single field of a struct.
4 The result is that you will index it's member directly.
6 With `#[index]` or `#[index(ignore)]` it's possible to indicate the field that
7 you want to derive `Index` for.
15 # use derive_more::Index;
18 struct MyVec(Vec<i32>);
20 // You can specify the field you want to derive Index for
28 assert_eq!(5, MyVec(vec![5, 8])[0]);
29 assert_eq!(200, Numbers { numbers: vec![100, 200], useless: false }[1]);
37 When deriving `Index` for a struct:
40 # use derive_more::Index;
50 Code like this will be generated:
57 impl<__IdxT> ::core::ops::Index<__IdxT> for Numbers
59 Vec<i32>: ::core::ops::Index<__IdxT>,
61 type Output = <Vec<i32> as ::core::ops::Index<__IdxT>>::Output;
63 fn index(&self, idx: __IdxT) -> &Self::Output {
64 <Vec<i32> as ::core::ops::Index<__IdxT>>::index(&self.numbers, idx)
74 Deriving `Index` is not supported for enums.