提交 451cc7e5 编写于 作者: J jbranchaud

Add doctests for union, diff, sym_diff, and intersection.

Add a rustdoc test for union to exhibit how it is used.

There is already a test for union in the test namespace, but this commit
adds a doctest that will appear in the rustdocs.

Add a doctest for the difference function.

Add a doctest for the symmetric_difference function.

Add a doctest for the intersection function.

Update the union et al. doctests based on @Gankro's comments.

Make the union et al. doctests a bit more readable.
上级 3a325c66
......@@ -94,12 +94,46 @@ pub fn into_iter(self) -> MoveItems<T> {
impl<T: Ord> BTreeSet<T> {
/// Visits the values representing the difference, in ascending order.
///
/// # Example
///
/// ```
/// use std::collections::BTreeSet;
///
/// let mut a = BTreeSet::new();
/// a.insert(1u);
/// a.insert(2u);
///
/// let mut b = BTreeSet::new();
/// b.insert(2u);
/// b.insert(3u);
///
/// let diff: Vec<uint> = a.difference(&b).cloned().collect();
/// assert_eq!(diff, vec![1u]);
/// ```
#[unstable = "matches collection reform specification, waiting for dust to settle"]
pub fn difference<'a>(&'a self, other: &'a BTreeSet<T>) -> DifferenceItems<'a, T> {
DifferenceItems{a: self.iter().peekable(), b: other.iter().peekable()}
}
/// Visits the values representing the symmetric difference, in ascending order.
///
/// # Example
///
/// ```
/// use std::collections::BTreeSet;
///
/// let mut a = BTreeSet::new();
/// a.insert(1u);
/// a.insert(2u);
///
/// let mut b = BTreeSet::new();
/// b.insert(2u);
/// b.insert(3u);
///
/// let sym_diff: Vec<uint> = a.symmetric_difference(&b).cloned().collect();
/// assert_eq!(sym_diff, vec![1u,3]);
/// ```
#[unstable = "matches collection reform specification, waiting for dust to settle"]
pub fn symmetric_difference<'a>(&'a self, other: &'a BTreeSet<T>)
-> SymDifferenceItems<'a, T> {
......@@ -107,6 +141,23 @@ pub fn symmetric_difference<'a>(&'a self, other: &'a BTreeSet<T>)
}
/// Visits the values representing the intersection, in ascending order.
///
/// # Example
///
/// ```
/// use std::collections::BTreeSet;
///
/// let mut a = BTreeSet::new();
/// a.insert(1u);
/// a.insert(2u);
///
/// let mut b = BTreeSet::new();
/// b.insert(2u);
/// b.insert(3u);
///
/// let intersection: Vec<uint> = a.intersection(&b).cloned().collect();
/// assert_eq!(intersection, vec![2u]);
/// ```
#[unstable = "matches collection reform specification, waiting for dust to settle"]
pub fn intersection<'a>(&'a self, other: &'a BTreeSet<T>)
-> IntersectionItems<'a, T> {
......@@ -114,6 +165,21 @@ pub fn intersection<'a>(&'a self, other: &'a BTreeSet<T>)
}
/// Visits the values representing the union, in ascending order.
///
/// # Example
///
/// ```
/// use std::collections::BTreeSet;
///
/// let mut a = BTreeSet::new();
/// a.insert(1u);
///
/// let mut b = BTreeSet::new();
/// b.insert(2u);
///
/// let union: Vec<uint> = a.union(&b).cloned().collect();
/// assert_eq!(union, vec![1u,2]);
/// ```
#[unstable = "matches collection reform specification, waiting for dust to settle"]
pub fn union<'a>(&'a self, other: &'a BTreeSet<T>) -> UnionItems<'a, T> {
UnionItems{a: self.iter().peekable(), b: other.iter().peekable()}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册