@@ -993,6 +993,28 @@ impl<K, V, S> IntoIterator for IndexMap<K, V, S> {
993993 }
994994}
995995
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+ /// ```
9961018impl < K , V , Q : ?Sized , S > Index < & Q > for IndexMap < K , V , S >
9971019where
9981020 Q : Hash + Equivalent < K > ,
@@ -1001,28 +1023,138 @@ where
10011023{
10021024 type Output = V ;
10031025
1026+ /// Returns a reference to the value corresponding to the supplied `key`.
1027+ ///
10041028 /// ***Panics*** if `key` is not present in the map.
10051029 fn index ( & self , key : & Q ) -> & V {
10061030 self . get ( key) . expect ( "IndexMap: key not found" )
10071031 }
10081032}
10091033
1034+ /// Access `IndexMap` values corresponding to a key.
1035+ ///
10101036/// Mutable indexing allows changing / updating values of key-value
10111037/// pairs that are already present.
10121038///
10131039/// 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+ /// ```
10141063impl < K , V , Q : ?Sized , S > IndexMut < & Q > for IndexMap < K , V , S >
10151064where
10161065 Q : Hash + Equivalent < K > ,
10171066 K : Hash + Eq ,
10181067 S : BuildHasher ,
10191068{
1069+ /// Returns a mutable reference to the value corresponding to the supplied `key`.
1070+ ///
10201071 /// ***Panics*** if `key` is not present in the map.
10211072 fn index_mut ( & mut self , key : & Q ) -> & mut V {
10221073 self . get_mut ( key) . expect ( "IndexMap: key not found" )
10231074 }
10241075}
10251076
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+ /// ```
1105+ impl < K , V , S > Index < usize > for IndexMap < K , V , S > {
1106+ type Output = V ;
1107+
1108+ /// Returns a reference to the value at the supplied `index`.
1109+ ///
1110+ /// ***Panics*** if `index` is out of bounds.
1111+ fn index ( & self , index : usize ) -> & V {
1112+ self . get_index ( index)
1113+ . expect ( "IndexMap: index out of bounds" )
1114+ . 1
1115+ }
1116+ }
1117+
1118+ /// Access `IndexMap` values at indexed positions.
1119+ ///
1120+ /// Mutable indexing allows changing / updating indexed values
1121+ /// that are already present.
1122+ ///
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+ /// ```
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+ ///
1150+ /// ***Panics*** if `index` is out of bounds.
1151+ fn index_mut ( & mut self , index : usize ) -> & mut V {
1152+ self . get_index_mut ( index)
1153+ . expect ( "IndexMap: index out of bounds" )
1154+ . 1
1155+ }
1156+ }
1157+
10261158impl < K , V , S > FromIterator < ( K , V ) > for IndexMap < K , V , S >
10271159where
10281160 K : Hash + Eq ,
0 commit comments