Skip to content

Commit 7cb46d3

Browse files
committed
Add documentation and examples for Index and IndexMut
1 parent bfd71da commit 7cb46d3

File tree

2 files changed

+138
-0
lines changed

2 files changed

+138
-0
lines changed

src/map.rs

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
/// ```
9821004
impl<K, V, Q: ?Sized, S> Index<&Q> for IndexMap<K, V, S>
9831005
where
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+
/// ```
10001049
impl<K, V, Q: ?Sized, S> IndexMut<&Q> for IndexMap<K, V, S>
10011050
where
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+
/// ```
10121091
impl<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+
/// ```
10271133
impl<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)

src/set.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,9 +592,39 @@ impl<T, S> IndexSet<T, S> {
592592
}
593593
}
594594

595+
/// Access `IndexSet` values at indexed positions.
596+
///
597+
/// # Examples
598+
///
599+
/// ```
600+
/// use indexmap::IndexSet;
601+
///
602+
/// let mut set = IndexSet::new();
603+
/// for word in "Lorem ipsum dolor sit amet".split_whitespace() {
604+
/// set.insert(word.to_string());
605+
/// }
606+
/// assert_eq!(set[0], "Lorem");
607+
/// assert_eq!(set[1], "ipsum");
608+
/// set.reverse();
609+
/// assert_eq!(set[0], "amet");
610+
/// assert_eq!(set[1], "sit");
611+
/// set.sort();
612+
/// assert_eq!(set[0], "Lorem");
613+
/// assert_eq!(set[1], "amet");
614+
/// ```
615+
///
616+
/// ```should_panic
617+
/// use indexmap::IndexSet;
618+
///
619+
/// let mut set = IndexSet::new();
620+
/// set.insert("foo");
621+
/// println!("{:?}", set[10]); // panics!
622+
/// ```
595623
impl<T, S> Index<usize> for IndexSet<T, S> {
596624
type Output = T;
597625

626+
/// Returns a reference to the value at the supplied `index`.
627+
///
598628
/// ***Panics*** if `index` is out of bounds.
599629
fn index(&self, index: usize) -> &T {
600630
self.get_index(index)

0 commit comments

Comments
 (0)