no bug - Correct some typos in the comments. a=typo-fix
[gecko.git] / third_party / rust / derive_more-impl / doc / index.md
blobc45fb46df5ec23ad0e581ab354b835e5bc87dada
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.
12 ## Example usage
14 ```rust
15 # use derive_more::Index;
17 #[derive(Index)]
18 struct MyVec(Vec<i32>);
20 // You can specify the field you want to derive Index for
21 #[derive(Index)]
22 struct Numbers {
23     #[index]
24     numbers: Vec<i32>,
25     useless: bool,
28 assert_eq!(5, MyVec(vec![5, 8])[0]);
29 assert_eq!(200, Numbers { numbers: vec![100, 200], useless: false }[1]);
30 ```
35 ## Structs
37 When deriving `Index` for a struct:
39 ```rust
40 # use derive_more::Index;
42 #[derive(Index)]
43 struct Numbers {
44     #[index]
45     numbers: Vec<i32>,
46     useless: bool,
48 ```
50 Code like this will be generated:
52 ```rust
53 # struct Numbers {
54 #     numbers: Vec<i32>,
55 #     useless: bool,
56 # }
57 impl<__IdxT> ::core::ops::Index<__IdxT> for Numbers
58 where
59     Vec<i32>: ::core::ops::Index<__IdxT>,
61     type Output = <Vec<i32> as ::core::ops::Index<__IdxT>>::Output;
62     #[inline]
63     fn index(&self, idx: __IdxT) -> &Self::Output {
64         <Vec<i32> as ::core::ops::Index<__IdxT>>::index(&self.numbers, idx)
65     }
67 ```
72 ## Enums
74 Deriving `Index` is not supported for enums.