提交 e7192c1a 编写于 作者: K kennytm 提交者: GitHub

Rollup merge of #47217 - stjepang:set-examples, r=frewsxcv

Write examples for {BTree,Hash}Set::{get,replace,take}
......@@ -415,6 +415,16 @@ pub fn contains<Q: ?Sized>(&self, value: &Q) -> bool
/// The value may be any borrowed form of the set's value type,
/// but the ordering on the borrowed form *must* match the
/// ordering on the value type.
///
/// # Examples
///
/// ```
/// use std::collections::BTreeSet;
///
/// let set: BTreeSet<_> = [1, 2, 3].iter().cloned().collect();
/// assert_eq!(set.get(&2), Some(&2));
/// assert_eq!(set.get(&4), None);
/// ```
#[stable(feature = "set_recovery", since = "1.9.0")]
pub fn get<Q: ?Sized>(&self, value: &Q) -> Option<&T>
where T: Borrow<Q>,
......@@ -540,6 +550,19 @@ pub fn insert(&mut self, value: T) -> bool {
/// Adds a value to the set, replacing the existing value, if any, that is equal to the given
/// one. Returns the replaced value.
///
/// # Examples
///
/// ```
/// use std::collections::BTreeSet;
///
/// let mut set = BTreeSet::new();
/// set.insert(Vec::<i32>::new());
///
/// assert_eq!(set.get(&[][..]).unwrap().capacity(), 0);
/// set.replace(Vec::with_capacity(10));
/// assert_eq!(set.get(&[][..]).unwrap().capacity(), 10);
/// ```
#[stable(feature = "set_recovery", since = "1.9.0")]
pub fn replace(&mut self, value: T) -> Option<T> {
Recover::replace(&mut self.map, value)
......@@ -576,6 +599,16 @@ pub fn remove<Q: ?Sized>(&mut self, value: &Q) -> bool
/// The value may be any borrowed form of the set's value type,
/// but the ordering on the borrowed form *must* match the
/// ordering on the value type.
///
/// # Examples
///
/// ```
/// use std::collections::BTreeSet;
///
/// let mut set: BTreeSet<_> = [1, 2, 3].iter().cloned().collect();
/// assert_eq!(set.take(&2), Some(2));
/// assert_eq!(set.take(&2), None);
/// ```
#[stable(feature = "set_recovery", since = "1.9.0")]
pub fn take<Q: ?Sized>(&mut self, value: &Q) -> Option<T>
where T: Borrow<Q>,
......
......@@ -527,6 +527,16 @@ pub fn contains<Q: ?Sized>(&self, value: &Q) -> bool
/// [`Hash`] and [`Eq`] on the borrowed form *must* match those for
/// the value type.
///
/// # Examples
///
/// ```
/// use std::collections::HashSet;
///
/// let set: HashSet<_> = [1, 2, 3].iter().cloned().collect();
/// assert_eq!(set.get(&2), Some(&2));
/// assert_eq!(set.get(&4), None);
/// ```
///
/// [`Eq`]: ../../std/cmp/trait.Eq.html
/// [`Hash`]: ../../std/hash/trait.Hash.html
#[stable(feature = "set_recovery", since = "1.9.0")]
......@@ -631,6 +641,19 @@ pub fn insert(&mut self, value: T) -> bool {
/// Adds a value to the set, replacing the existing value, if any, that is equal to the given
/// one. Returns the replaced value.
///
/// # Examples
///
/// ```
/// use std::collections::HashSet;
///
/// let mut set = HashSet::new();
/// set.insert(Vec::<i32>::new());
///
/// assert_eq!(set.get(&[][..]).unwrap().capacity(), 0);
/// set.replace(Vec::with_capacity(10));
/// assert_eq!(set.get(&[][..]).unwrap().capacity(), 10);
/// ```
#[stable(feature = "set_recovery", since = "1.9.0")]
pub fn replace(&mut self, value: T) -> Option<T> {
Recover::replace(&mut self.map, value)
......@@ -671,6 +694,16 @@ pub fn remove<Q: ?Sized>(&mut self, value: &Q) -> bool
/// [`Hash`] and [`Eq`] on the borrowed form *must* match those for
/// the value type.
///
/// # Examples
///
/// ```
/// use std::collections::HashSet;
///
/// let mut set: HashSet<_> = [1, 2, 3].iter().cloned().collect();
/// assert_eq!(set.take(&2), Some(2));
/// assert_eq!(set.take(&2), None);
/// ```
///
/// [`Eq`]: ../../std/cmp/trait.Eq.html
/// [`Hash`]: ../../std/hash/trait.Hash.html
#[stable(feature = "set_recovery", since = "1.9.0")]
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册