Skip to content

Add a doctest for the entry method of BTreeMap. #19711

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/libcollections/btree/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1156,6 +1156,23 @@ impl<K, V> BTreeMap<K, V> {

impl<K: Ord, V> BTreeMap<K, V> {
/// Gets the given key's corresponding entry in the map for in-place manipulation.
///
/// # Examples
///
/// ```
/// use std::collections::BTreeMap;
/// use std::collections::btree_map::Entry;
///
/// let mut map = BTreeMap::new();
/// map.insert("a", 1u);
/// map.insert("b", 2u);
/// map.insert("c", 3u);
///
/// match map.entry("b") {
/// Entry::Vacant(_) => unreachable!(),
/// Entry::Occupied(view) => assert_eq!(*view.get(), 2u),
/// }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't very idiomatic "entry" code, to be honest. Generally when you're working with entry you're genuinely uncertain if the value is contained, and have something useful to do in both cases. If you "know" the value is-or-isn't there, you should be using indexing or insert.

See the top-level collection docs for some examples I wrote.

/// ```
pub fn entry<'a>(&'a mut self, key: K) -> Entry<'a, K, V> {
// same basic logic of `swap` and `pop`, blended together
let mut stack = stack::PartialSearchStack::new(self);
Expand Down