Skip to content

Commit 7948f45

Browse files
author
Stjepan Glavina
committed
Write examples for {BTree,Hash}Set::{get,replace,take}
1 parent dd582ac commit 7948f45

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

src/liballoc/btree/set.rs

+33
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,16 @@ impl<T: Ord> BTreeSet<T> {
415415
/// The value may be any borrowed form of the set's value type,
416416
/// but the ordering on the borrowed form *must* match the
417417
/// ordering on the value type.
418+
///
419+
/// # Examples
420+
///
421+
/// ```
422+
/// use std::collections::BTreeSet;
423+
///
424+
/// let set: BTreeSet<_> = [1, 2, 3].iter().cloned().collect();
425+
/// assert_eq!(set.get(&2), Some(&2));
426+
/// assert_eq!(set.get(&4), None);
427+
/// ```
418428
#[stable(feature = "set_recovery", since = "1.9.0")]
419429
pub fn get<Q: ?Sized>(&self, value: &Q) -> Option<&T>
420430
where T: Borrow<Q>,
@@ -540,6 +550,19 @@ impl<T: Ord> BTreeSet<T> {
540550

541551
/// Adds a value to the set, replacing the existing value, if any, that is equal to the given
542552
/// one. Returns the replaced value.
553+
///
554+
/// # Examples
555+
///
556+
/// ```
557+
/// use std::collections::BTreeSet;
558+
///
559+
/// let mut set = BTreeSet::new();
560+
/// set.insert(Vec::<i32>::new());
561+
///
562+
/// assert_eq!(set.get(&[][..]).unwrap().capacity(), 0);
563+
/// set.replace(Vec::with_capacity(10));
564+
/// assert_eq!(set.get(&[][..]).unwrap().capacity(), 10);
565+
/// ```
543566
#[stable(feature = "set_recovery", since = "1.9.0")]
544567
pub fn replace(&mut self, value: T) -> Option<T> {
545568
Recover::replace(&mut self.map, value)
@@ -576,6 +599,16 @@ impl<T: Ord> BTreeSet<T> {
576599
/// The value may be any borrowed form of the set's value type,
577600
/// but the ordering on the borrowed form *must* match the
578601
/// ordering on the value type.
602+
///
603+
/// # Examples
604+
///
605+
/// ```
606+
/// use std::collections::BTreeSet;
607+
///
608+
/// let mut set: BTreeSet<_> = [1, 2, 3].iter().cloned().collect();
609+
/// assert_eq!(set.take(&2), Some(2));
610+
/// assert_eq!(set.take(&2), None);
611+
/// ```
579612
#[stable(feature = "set_recovery", since = "1.9.0")]
580613
pub fn take<Q: ?Sized>(&mut self, value: &Q) -> Option<T>
581614
where T: Borrow<Q>,

src/libstd/collections/hash/set.rs

+33
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,16 @@ impl<T, S> HashSet<T, S>
527527
/// [`Hash`] and [`Eq`] on the borrowed form *must* match those for
528528
/// the value type.
529529
///
530+
/// # Examples
531+
///
532+
/// ```
533+
/// use std::collections::HashSet;
534+
///
535+
/// let set: HashSet<_> = [1, 2, 3].iter().cloned().collect();
536+
/// assert_eq!(set.get(&2), Some(&2));
537+
/// assert_eq!(set.get(&4), None);
538+
/// ```
539+
///
530540
/// [`Eq`]: ../../std/cmp/trait.Eq.html
531541
/// [`Hash`]: ../../std/hash/trait.Hash.html
532542
#[stable(feature = "set_recovery", since = "1.9.0")]
@@ -631,6 +641,19 @@ impl<T, S> HashSet<T, S>
631641

632642
/// Adds a value to the set, replacing the existing value, if any, that is equal to the given
633643
/// one. Returns the replaced value.
644+
///
645+
/// # Examples
646+
///
647+
/// ```
648+
/// use std::collections::HashSet;
649+
///
650+
/// let mut set = HashSet::new();
651+
/// set.insert(Vec::<i32>::new());
652+
///
653+
/// assert_eq!(set.get(&[][..]).unwrap().capacity(), 0);
654+
/// set.replace(Vec::with_capacity(10));
655+
/// assert_eq!(set.get(&[][..]).unwrap().capacity(), 10);
656+
/// ```
634657
#[stable(feature = "set_recovery", since = "1.9.0")]
635658
pub fn replace(&mut self, value: T) -> Option<T> {
636659
Recover::replace(&mut self.map, value)
@@ -671,6 +694,16 @@ impl<T, S> HashSet<T, S>
671694
/// [`Hash`] and [`Eq`] on the borrowed form *must* match those for
672695
/// the value type.
673696
///
697+
/// # Examples
698+
///
699+
/// ```
700+
/// use std::collections::HashSet;
701+
///
702+
/// let mut set: HashSet<_> = [1, 2, 3].iter().cloned().collect();
703+
/// assert_eq!(set.take(&2), Some(2));
704+
/// assert_eq!(set.take(&2), None);
705+
/// ```
706+
///
674707
/// [`Eq`]: ../../std/cmp/trait.Eq.html
675708
/// [`Hash`]: ../../std/hash/trait.Hash.html
676709
#[stable(feature = "set_recovery", since = "1.9.0")]

0 commit comments

Comments
 (0)