Skip to content

Commit

Permalink
Rollup merge of rust-lang#47839 - frewsxcv:frewsxcv-map-index, r=Quie…
Browse files Browse the repository at this point in the history
…tMisdreavus

Document that `Index` ops can panic on `HashMap` & `BTreeMap`.

Fixes rust-lang#47011.
  • Loading branch information
kennytm authored Jan 30, 2018
2 parents e8868bd + 7b4cbbd commit 95572df
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/liballoc/btree/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1748,6 +1748,11 @@ impl<'a, K: Ord, Q: ?Sized, V> Index<&'a Q> for BTreeMap<K, V>
{
type Output = V;

/// Returns a reference to the value corresponding to the supplied key.
///
/// # Panics
///
/// Panics if the key is not present in the `BTreeMap`.
#[inline]
fn index(&self, key: &Q) -> &V {
self.get(key).expect("no entry found for key")
Expand Down
9 changes: 7 additions & 2 deletions src/libstd/collections/hash/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1384,9 +1384,14 @@ impl<'a, K, Q: ?Sized, V, S> Index<&'a Q> for HashMap<K, V, S>
{
type Output = V;

/// Returns a reference to the value corresponding to the supplied key.
///
/// # Panics
///
/// Panics if the key is not present in the `HashMap`.
#[inline]
fn index(&self, index: &Q) -> &V {
self.get(index).expect("no entry found for key")
fn index(&self, key: &Q) -> &V {
self.get(key).expect("no entry found for key")
}
}

Expand Down

0 comments on commit 95572df

Please sign in to comment.