Skip to content

Commit 4d4fc0a

Browse files
committed
Add documentation and examples for Index and IndexMut
1 parent 7378003 commit 4d4fc0a

File tree

2 files changed

+138
-0
lines changed

2 files changed

+138
-0
lines changed

src/map.rs

+108
Original file line numberDiff line numberDiff line change
@@ -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+
/// ```
9961018
impl<K, V, Q: ?Sized, S> Index<&Q> for IndexMap<K, V, S>
9971019
where
9981020
Q: Hash + Equivalent<K>,
@@ -1001,31 +1023,90 @@ 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+
/// ```
10141063
impl<K, V, Q: ?Sized, S> IndexMut<&Q> for IndexMap<K, V, S>
10151064
where
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+
/// ```
10261105
impl<K, V, S> Index<usize> for IndexMap<K, V, S> {
10271106
type Output = V;
10281107

1108+
/// Returns a reference to the value at the supplied `index`.
1109+
///
10291110
/// ***Panics*** if `index` is out of bounds.
10301111
fn index(&self, index: usize) -> &V {
10311112
self.get_index(index)
@@ -1034,11 +1115,38 @@ impl<K, V, S> Index<usize> for IndexMap<K, V, S> {
10341115
}
10351116
}
10361117

1118+
/// Access `IndexMap` values at indexed positions.
1119+
///
10371120
/// Mutable indexing allows changing / updating indexed values
10381121
/// that are already present.
10391122
///
10401123
/// 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+
/// ```
10411147
impl<K, V, S> IndexMut<usize> for IndexMap<K, V, S> {
1148+
/// Returns a mutable reference to the value at the supplied `index`.
1149+
///
10421150
/// ***Panics*** if `index` is out of bounds.
10431151
fn index_mut(&mut self, index: usize) -> &mut V {
10441152
self.get_index_mut(index)

src/set.rs

+30
Original file line numberDiff line numberDiff line change
@@ -606,9 +606,39 @@ impl<T, S> IndexSet<T, S> {
606606
}
607607
}
608608

609+
/// Access `IndexSet` values at indexed positions.
610+
///
611+
/// # Examples
612+
///
613+
/// ```
614+
/// use indexmap::IndexSet;
615+
///
616+
/// let mut set = IndexSet::new();
617+
/// for word in "Lorem ipsum dolor sit amet".split_whitespace() {
618+
/// set.insert(word.to_string());
619+
/// }
620+
/// assert_eq!(set[0], "Lorem");
621+
/// assert_eq!(set[1], "ipsum");
622+
/// set.reverse();
623+
/// assert_eq!(set[0], "amet");
624+
/// assert_eq!(set[1], "sit");
625+
/// set.sort();
626+
/// assert_eq!(set[0], "Lorem");
627+
/// assert_eq!(set[1], "amet");
628+
/// ```
629+
///
630+
/// ```should_panic
631+
/// use indexmap::IndexSet;
632+
///
633+
/// let mut set = IndexSet::new();
634+
/// set.insert("foo");
635+
/// println!("{:?}", set[10]); // panics!
636+
/// ```
609637
impl<T, S> Index<usize> for IndexSet<T, S> {
610638
type Output = T;
611639

640+
/// Returns a reference to the value at the supplied `index`.
641+
///
612642
/// ***Panics*** if `index` is out of bounds.
613643
fn index(&self, index: usize) -> &T {
614644
self.get_index(index)

0 commit comments

Comments
 (0)