Skip to content

Commit 7b4cbbd

Browse files
committed
Document that Index ops can panic on HashMap & BTreeMap.
Fixes #47011.
1 parent 21882aa commit 7b4cbbd

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/liballoc/btree/map.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1748,6 +1748,11 @@ impl<'a, K: Ord, Q: ?Sized, V> Index<&'a Q> for BTreeMap<K, V>
17481748
{
17491749
type Output = V;
17501750

1751+
/// Returns a reference to the value corresponding to the supplied key.
1752+
///
1753+
/// # Panics
1754+
///
1755+
/// Panics if the key is not present in the `BTreeMap`.
17511756
#[inline]
17521757
fn index(&self, key: &Q) -> &V {
17531758
self.get(key).expect("no entry found for key")

src/libstd/collections/hash/map.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -1384,9 +1384,14 @@ impl<'a, K, Q: ?Sized, V, S> Index<&'a Q> for HashMap<K, V, S>
13841384
{
13851385
type Output = V;
13861386

1387+
/// Returns a reference to the value corresponding to the supplied key.
1388+
///
1389+
/// # Panics
1390+
///
1391+
/// Panics if the key is not present in the `HashMap`.
13871392
#[inline]
1388-
fn index(&self, index: &Q) -> &V {
1389-
self.get(index).expect("no entry found for key")
1393+
fn index(&self, key: &Q) -> &V {
1394+
self.get(key).expect("no entry found for key")
13901395
}
13911396
}
13921397

0 commit comments

Comments
 (0)