diff options
| author | Josh Stone <jistone@redhat.com> | 2024-11-27 12:22:21 -0800 |
|---|---|---|
| committer | Josh Stone <jistone@redhat.com> | 2024-11-27 12:22:21 -0800 |
| commit | 21b1ab1779a6f58f4a77bad0e1d3a21efcd9bc47 (patch) | |
| tree | 5a2bf8c185df3bea18e9313fc59d33c1d938bb54 | |
| parent | 82b8ea8a7a63ccc9be8e6c557fc4b2fb2cd14145 (diff) | |
| download | rust-21b1ab1779a6f58f4a77bad0e1d3a21efcd9bc47.tar.gz rust-21b1ab1779a6f58f4a77bad0e1d3a21efcd9bc47.zip | |
Fill in a `BTreeSet::entry` example
| -rw-r--r-- | library/alloc/src/collections/btree/set.rs | 32 |
1 files changed, 31 insertions, 1 deletions
diff --git a/library/alloc/src/collections/btree/set.rs b/library/alloc/src/collections/btree/set.rs index a9c64fd41d5..6f8c3b2d152 100644 --- a/library/alloc/src/collections/btree/set.rs +++ b/library/alloc/src/collections/btree/set.rs @@ -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> |
