Skip to content

Commit

Permalink
Fill in a BTreeSet::entry example
Browse files Browse the repository at this point in the history
  • Loading branch information
cuviper authored and gitbot committed Feb 20, 2025
1 parent eabfb8d commit 3a9be70
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion alloc/src/collections/btree/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -993,7 +993,37 @@ impl<T, A: Allocator + Clone> BTreeSet<T, A> {
///
/// # Examples
///
/// TODO
/// ```
/// #![feature(btree_set_entry)]
///
/// use std::collections::BTreeSet;
/// use std::collections::btree_set::Entry::*;
///
/// let mut singles = BTreeSet::new();
/// let mut dupes = BTreeSet::new();
///
/// for ch in "a short treatise on fungi".chars() {
/// if let Vacant(dupe_entry) = dupes.entry(ch) {
/// // We haven't already seen a duplicate, so
/// // check if we've at least seen it once.
/// match singles.entry(ch) {
/// Vacant(single_entry) => {
/// // We found a new character for the first time.
/// single_entry.insert()
/// }
/// Occupied(single_entry) => {
/// // We've already seen this once, "move" it to dupes.
/// single_entry.remove();
/// dupe_entry.insert();
/// }
/// }
/// }
/// }
///
/// assert!(!singles.contains(&'t') && dupes.contains(&'t'));
/// assert!(singles.contains(&'u') && !dupes.contains(&'u'));
/// assert!(!singles.contains(&'v') && !dupes.contains(&'v'));
/// ```
#[inline]
#[unstable(feature = "btree_set_entry", issue = "133549")]
pub fn entry(&mut self, value: T) -> Entry<'_, T, A>
Expand Down

0 comments on commit 3a9be70

Please sign in to comment.