@@ -979,6 +979,28 @@ impl<K, V, S> IntoIterator for IndexMap<K, V, S> {
979979 }
980980}
981981
982+ /// Access `IndexMap` values corresponding to a key.
983+ ///
984+ /// # Examples
985+ ///
986+ /// ```
987+ /// use indexmap::IndexMap;
988+ ///
989+ /// let mut map = IndexMap::new();
990+ /// for word in "Lorem ipsum dolor sit amet".split_whitespace() {
991+ /// map.insert(word.to_lowercase(), word.to_uppercase());
992+ /// }
993+ /// assert_eq!(map["lorem"], "LOREM");
994+ /// assert_eq!(map["ipsum"], "IPSUM");
995+ /// ```
996+ ///
997+ /// ```should_panic
998+ /// use indexmap::IndexMap;
999+ ///
1000+ /// let mut map = IndexMap::new();
1001+ /// map.insert("foo", 1);
1002+ /// println!("{:?}", map["bar"]); // panics!
1003+ /// ```
9821004impl < K , V , Q : ?Sized , S > Index < & Q > for IndexMap < K , V , S >
9831005where
9841006 Q : Hash + Equivalent < K > ,
@@ -987,31 +1009,90 @@ where
9871009{
9881010 type Output = V ;
9891011
1012+ /// Returns a reference to the value corresponding to the supplied `key`.
1013+ ///
9901014 /// ***Panics*** if `key` is not present in the map.
9911015 fn index ( & self , key : & Q ) -> & V {
9921016 self . get ( key) . expect ( "IndexMap: key not found" )
9931017 }
9941018}
9951019
1020+ /// Access `IndexMap` values corresponding to a key.
1021+ ///
9961022/// Mutable indexing allows changing / updating values of key-value
9971023/// pairs that are already present.
9981024///
9991025/// You can **not** insert new pairs with index syntax, use `.insert()`.
1026+ ///
1027+ /// # Examples
1028+ ///
1029+ /// ```
1030+ /// use indexmap::IndexMap;
1031+ ///
1032+ /// let mut map = IndexMap::new();
1033+ /// for word in "Lorem ipsum dolor sit amet".split_whitespace() {
1034+ /// map.insert(word.to_lowercase(), word.to_string());
1035+ /// }
1036+ /// let lorem = &mut map["lorem"];
1037+ /// assert_eq!(lorem, "Lorem");
1038+ /// lorem.retain(char::is_lowercase);
1039+ /// assert_eq!(map["lorem"], "orem");
1040+ /// ```
1041+ ///
1042+ /// ```should_panic
1043+ /// use indexmap::IndexMap;
1044+ ///
1045+ /// let mut map = IndexMap::new();
1046+ /// map.insert("foo", 1);
1047+ /// map["bar"] = 1; // panics!
1048+ /// ```
10001049impl < K , V , Q : ?Sized , S > IndexMut < & Q > for IndexMap < K , V , S >
10011050where
10021051 Q : Hash + Equivalent < K > ,
10031052 K : Hash + Eq ,
10041053 S : BuildHasher ,
10051054{
1055+ /// Returns a mutable reference to the value corresponding to the supplied `key`.
1056+ ///
10061057 /// ***Panics*** if `key` is not present in the map.
10071058 fn index_mut ( & mut self , key : & Q ) -> & mut V {
10081059 self . get_mut ( key) . expect ( "IndexMap: key not found" )
10091060 }
10101061}
10111062
1063+ /// Access `IndexMap` values at indexed positions.
1064+ ///
1065+ /// # Examples
1066+ ///
1067+ /// ```
1068+ /// use indexmap::IndexMap;
1069+ ///
1070+ /// let mut map = IndexMap::new();
1071+ /// for word in "Lorem ipsum dolor sit amet".split_whitespace() {
1072+ /// map.insert(word.to_lowercase(), word.to_uppercase());
1073+ /// }
1074+ /// assert_eq!(map[0], "LOREM");
1075+ /// assert_eq!(map[1], "IPSUM");
1076+ /// map.reverse();
1077+ /// assert_eq!(map[0], "AMET");
1078+ /// assert_eq!(map[1], "SIT");
1079+ /// map.sort_keys();
1080+ /// assert_eq!(map[0], "AMET");
1081+ /// assert_eq!(map[1], "DOLOR");
1082+ /// ```
1083+ ///
1084+ /// ```should_panic
1085+ /// use indexmap::IndexMap;
1086+ ///
1087+ /// let mut map = IndexMap::new();
1088+ /// map.insert("foo", 1);
1089+ /// println!("{:?}", map[10]); // panics!
1090+ /// ```
10121091impl < K , V , S > Index < usize > for IndexMap < K , V , S > {
10131092 type Output = V ;
10141093
1094+ /// Returns a reference to the value at the supplied `index`.
1095+ ///
10151096 /// ***Panics*** if `index` is out of bounds.
10161097 fn index ( & self , index : usize ) -> & V {
10171098 self . get_index ( index)
@@ -1020,11 +1101,38 @@ impl<K, V, S> Index<usize> for IndexMap<K, V, S> {
10201101 }
10211102}
10221103
1104+ /// Access `IndexMap` values at indexed positions.
1105+ ///
10231106/// Mutable indexing allows changing / updating indexed values
10241107/// that are already present.
10251108///
10261109/// You can **not** insert new values with index syntax, use `.insert()`.
1110+ ///
1111+ /// # Examples
1112+ ///
1113+ /// ```
1114+ /// use indexmap::IndexMap;
1115+ ///
1116+ /// let mut map = IndexMap::new();
1117+ /// for word in "Lorem ipsum dolor sit amet".split_whitespace() {
1118+ /// map.insert(word.to_lowercase(), word.to_string());
1119+ /// }
1120+ /// let lorem = &mut map[0];
1121+ /// assert_eq!(lorem, "Lorem");
1122+ /// lorem.retain(char::is_lowercase);
1123+ /// assert_eq!(map["lorem"], "orem");
1124+ /// ```
1125+ ///
1126+ /// ```should_panic
1127+ /// use indexmap::IndexMap;
1128+ ///
1129+ /// let mut map = IndexMap::new();
1130+ /// map.insert("foo", 1);
1131+ /// map[10] = 1; // panics!
1132+ /// ```
10271133impl < K , V , S > IndexMut < usize > for IndexMap < K , V , S > {
1134+ /// Returns a mutable reference to the value at the supplied `index`.
1135+ ///
10281136 /// ***Panics*** if `index` is out of bounds.
10291137 fn index_mut ( & mut self , index : usize ) -> & mut V {
10301138 self . get_index_mut ( index)
0 commit comments