@@ -993,6 +993,28 @@ impl<K, V, S> IntoIterator for IndexMap<K, V, S> {
993
993
}
994
994
}
995
995
996
+ /// Access `IndexMap` values corresponding to a key.
997
+ ///
998
+ /// # Examples
999
+ ///
1000
+ /// ```
1001
+ /// use indexmap::IndexMap;
1002
+ ///
1003
+ /// let mut map = IndexMap::new();
1004
+ /// for word in "Lorem ipsum dolor sit amet".split_whitespace() {
1005
+ /// map.insert(word.to_lowercase(), word.to_uppercase());
1006
+ /// }
1007
+ /// assert_eq!(map["lorem"], "LOREM");
1008
+ /// assert_eq!(map["ipsum"], "IPSUM");
1009
+ /// ```
1010
+ ///
1011
+ /// ```should_panic
1012
+ /// use indexmap::IndexMap;
1013
+ ///
1014
+ /// let mut map = IndexMap::new();
1015
+ /// map.insert("foo", 1);
1016
+ /// println!("{:?}", map["bar"]); // panics!
1017
+ /// ```
996
1018
impl < K , V , Q : ?Sized , S > Index < & Q > for IndexMap < K , V , S >
997
1019
where
998
1020
Q : Hash + Equivalent < K > ,
@@ -1001,31 +1023,90 @@ where
1001
1023
{
1002
1024
type Output = V ;
1003
1025
1026
+ /// Returns a reference to the value corresponding to the supplied `key`.
1027
+ ///
1004
1028
/// ***Panics*** if `key` is not present in the map.
1005
1029
fn index ( & self , key : & Q ) -> & V {
1006
1030
self . get ( key) . expect ( "IndexMap: key not found" )
1007
1031
}
1008
1032
}
1009
1033
1034
+ /// Access `IndexMap` values corresponding to a key.
1035
+ ///
1010
1036
/// Mutable indexing allows changing / updating values of key-value
1011
1037
/// pairs that are already present.
1012
1038
///
1013
1039
/// You can **not** insert new pairs with index syntax, use `.insert()`.
1040
+ ///
1041
+ /// # Examples
1042
+ ///
1043
+ /// ```
1044
+ /// use indexmap::IndexMap;
1045
+ ///
1046
+ /// let mut map = IndexMap::new();
1047
+ /// for word in "Lorem ipsum dolor sit amet".split_whitespace() {
1048
+ /// map.insert(word.to_lowercase(), word.to_string());
1049
+ /// }
1050
+ /// let lorem = &mut map["lorem"];
1051
+ /// assert_eq!(lorem, "Lorem");
1052
+ /// lorem.retain(char::is_lowercase);
1053
+ /// assert_eq!(map["lorem"], "orem");
1054
+ /// ```
1055
+ ///
1056
+ /// ```should_panic
1057
+ /// use indexmap::IndexMap;
1058
+ ///
1059
+ /// let mut map = IndexMap::new();
1060
+ /// map.insert("foo", 1);
1061
+ /// map["bar"] = 1; // panics!
1062
+ /// ```
1014
1063
impl < K , V , Q : ?Sized , S > IndexMut < & Q > for IndexMap < K , V , S >
1015
1064
where
1016
1065
Q : Hash + Equivalent < K > ,
1017
1066
K : Hash + Eq ,
1018
1067
S : BuildHasher ,
1019
1068
{
1069
+ /// Returns a mutable reference to the value corresponding to the supplied `key`.
1070
+ ///
1020
1071
/// ***Panics*** if `key` is not present in the map.
1021
1072
fn index_mut ( & mut self , key : & Q ) -> & mut V {
1022
1073
self . get_mut ( key) . expect ( "IndexMap: key not found" )
1023
1074
}
1024
1075
}
1025
1076
1077
+ /// Access `IndexMap` values at indexed positions.
1078
+ ///
1079
+ /// # Examples
1080
+ ///
1081
+ /// ```
1082
+ /// use indexmap::IndexMap;
1083
+ ///
1084
+ /// let mut map = IndexMap::new();
1085
+ /// for word in "Lorem ipsum dolor sit amet".split_whitespace() {
1086
+ /// map.insert(word.to_lowercase(), word.to_uppercase());
1087
+ /// }
1088
+ /// assert_eq!(map[0], "LOREM");
1089
+ /// assert_eq!(map[1], "IPSUM");
1090
+ /// map.reverse();
1091
+ /// assert_eq!(map[0], "AMET");
1092
+ /// assert_eq!(map[1], "SIT");
1093
+ /// map.sort_keys();
1094
+ /// assert_eq!(map[0], "AMET");
1095
+ /// assert_eq!(map[1], "DOLOR");
1096
+ /// ```
1097
+ ///
1098
+ /// ```should_panic
1099
+ /// use indexmap::IndexMap;
1100
+ ///
1101
+ /// let mut map = IndexMap::new();
1102
+ /// map.insert("foo", 1);
1103
+ /// println!("{:?}", map[10]); // panics!
1104
+ /// ```
1026
1105
impl < K , V , S > Index < usize > for IndexMap < K , V , S > {
1027
1106
type Output = V ;
1028
1107
1108
+ /// Returns a reference to the value at the supplied `index`.
1109
+ ///
1029
1110
/// ***Panics*** if `index` is out of bounds.
1030
1111
fn index ( & self , index : usize ) -> & V {
1031
1112
self . get_index ( index)
@@ -1034,11 +1115,38 @@ impl<K, V, S> Index<usize> for IndexMap<K, V, S> {
1034
1115
}
1035
1116
}
1036
1117
1118
+ /// Access `IndexMap` values at indexed positions.
1119
+ ///
1037
1120
/// Mutable indexing allows changing / updating indexed values
1038
1121
/// that are already present.
1039
1122
///
1040
1123
/// You can **not** insert new values with index syntax, use `.insert()`.
1124
+ ///
1125
+ /// # Examples
1126
+ ///
1127
+ /// ```
1128
+ /// use indexmap::IndexMap;
1129
+ ///
1130
+ /// let mut map = IndexMap::new();
1131
+ /// for word in "Lorem ipsum dolor sit amet".split_whitespace() {
1132
+ /// map.insert(word.to_lowercase(), word.to_string());
1133
+ /// }
1134
+ /// let lorem = &mut map[0];
1135
+ /// assert_eq!(lorem, "Lorem");
1136
+ /// lorem.retain(char::is_lowercase);
1137
+ /// assert_eq!(map["lorem"], "orem");
1138
+ /// ```
1139
+ ///
1140
+ /// ```should_panic
1141
+ /// use indexmap::IndexMap;
1142
+ ///
1143
+ /// let mut map = IndexMap::new();
1144
+ /// map.insert("foo", 1);
1145
+ /// map[10] = 1; // panics!
1146
+ /// ```
1041
1147
impl < K , V , S > IndexMut < usize > for IndexMap < K , V , S > {
1148
+ /// Returns a mutable reference to the value at the supplied `index`.
1149
+ ///
1042
1150
/// ***Panics*** if `index` is out of bounds.
1043
1151
fn index_mut ( & mut self , index : usize ) -> & mut V {
1044
1152
self . get_index_mut ( index)
0 commit comments