@@ -979,6 +979,28 @@ impl<K, V, S> IntoIterator for IndexMap<K, V, S> {
979
979
}
980
980
}
981
981
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
+ /// ```
982
1004
impl < K , V , Q : ?Sized , S > Index < & Q > for IndexMap < K , V , S >
983
1005
where
984
1006
Q : Hash + Equivalent < K > ,
@@ -987,31 +1009,90 @@ where
987
1009
{
988
1010
type Output = V ;
989
1011
1012
+ /// Returns a reference to the value corresponding to the supplied `key`.
1013
+ ///
990
1014
/// ***Panics*** if `key` is not present in the map.
991
1015
fn index ( & self , key : & Q ) -> & V {
992
1016
self . get ( key) . expect ( "IndexMap: key not found" )
993
1017
}
994
1018
}
995
1019
1020
+ /// Access `IndexMap` values corresponding to a key.
1021
+ ///
996
1022
/// Mutable indexing allows changing / updating values of key-value
997
1023
/// pairs that are already present.
998
1024
///
999
1025
/// 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
+ /// ```
1000
1049
impl < K , V , Q : ?Sized , S > IndexMut < & Q > for IndexMap < K , V , S >
1001
1050
where
1002
1051
Q : Hash + Equivalent < K > ,
1003
1052
K : Hash + Eq ,
1004
1053
S : BuildHasher ,
1005
1054
{
1055
+ /// Returns a mutable reference to the value corresponding to the supplied `key`.
1056
+ ///
1006
1057
/// ***Panics*** if `key` is not present in the map.
1007
1058
fn index_mut ( & mut self , key : & Q ) -> & mut V {
1008
1059
self . get_mut ( key) . expect ( "IndexMap: key not found" )
1009
1060
}
1010
1061
}
1011
1062
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
+ /// ```
1012
1091
impl < K , V , S > Index < usize > for IndexMap < K , V , S > {
1013
1092
type Output = V ;
1014
1093
1094
+ /// Returns a reference to the value at the supplied `index`.
1095
+ ///
1015
1096
/// ***Panics*** if `index` is out of bounds.
1016
1097
fn index ( & self , index : usize ) -> & V {
1017
1098
self . get_index ( index)
@@ -1020,11 +1101,38 @@ impl<K, V, S> Index<usize> for IndexMap<K, V, S> {
1020
1101
}
1021
1102
}
1022
1103
1104
+ /// Access `IndexMap` values at indexed positions.
1105
+ ///
1023
1106
/// Mutable indexing allows changing / updating indexed values
1024
1107
/// that are already present.
1025
1108
///
1026
1109
/// 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
+ /// ```
1027
1133
impl < K , V , S > IndexMut < usize > for IndexMap < K , V , S > {
1134
+ /// Returns a mutable reference to the value at the supplied `index`.
1135
+ ///
1028
1136
/// ***Panics*** if `index` is out of bounds.
1029
1137
fn index_mut ( & mut self , index : usize ) -> & mut V {
1030
1138
self . get_index_mut ( index)
0 commit comments