Skip to content

Commit 1e835cc

Browse files
committed
auto merge of #19488 : jbranchaud/rust/add-btree-set-doctests, r=alexcrichton
There is already a test for `union` in the test namespace, but this commit adds a doctest that will appear in the rustdocs. Someone on IRC said, *Write doctests!*, so here I am. I am not sure this is the best way to demonstrate the behavior of the union function, so I am open to suggestions for improving this. If I am on the right track I'd be glad to include similar doctests for `intersection`, `difference`, etc.
2 parents f7d18b9 + 451cc7e commit 1e835cc

File tree

1 file changed

+66
-0
lines changed
  • src/libcollections/btree

1 file changed

+66
-0
lines changed

src/libcollections/btree/set.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,26 +94,92 @@ impl<T> BTreeSet<T> {
9494

9595
impl<T: Ord> BTreeSet<T> {
9696
/// Visits the values representing the difference, in ascending order.
97+
///
98+
/// # Example
99+
///
100+
/// ```
101+
/// use std::collections::BTreeSet;
102+
///
103+
/// let mut a = BTreeSet::new();
104+
/// a.insert(1u);
105+
/// a.insert(2u);
106+
///
107+
/// let mut b = BTreeSet::new();
108+
/// b.insert(2u);
109+
/// b.insert(3u);
110+
///
111+
/// let diff: Vec<uint> = a.difference(&b).cloned().collect();
112+
/// assert_eq!(diff, vec![1u]);
113+
/// ```
97114
#[unstable = "matches collection reform specification, waiting for dust to settle"]
98115
pub fn difference<'a>(&'a self, other: &'a BTreeSet<T>) -> DifferenceItems<'a, T> {
99116
DifferenceItems{a: self.iter().peekable(), b: other.iter().peekable()}
100117
}
101118

102119
/// Visits the values representing the symmetric difference, in ascending order.
120+
///
121+
/// # Example
122+
///
123+
/// ```
124+
/// use std::collections::BTreeSet;
125+
///
126+
/// let mut a = BTreeSet::new();
127+
/// a.insert(1u);
128+
/// a.insert(2u);
129+
///
130+
/// let mut b = BTreeSet::new();
131+
/// b.insert(2u);
132+
/// b.insert(3u);
133+
///
134+
/// let sym_diff: Vec<uint> = a.symmetric_difference(&b).cloned().collect();
135+
/// assert_eq!(sym_diff, vec![1u,3]);
136+
/// ```
103137
#[unstable = "matches collection reform specification, waiting for dust to settle"]
104138
pub fn symmetric_difference<'a>(&'a self, other: &'a BTreeSet<T>)
105139
-> SymDifferenceItems<'a, T> {
106140
SymDifferenceItems{a: self.iter().peekable(), b: other.iter().peekable()}
107141
}
108142

109143
/// Visits the values representing the intersection, in ascending order.
144+
///
145+
/// # Example
146+
///
147+
/// ```
148+
/// use std::collections::BTreeSet;
149+
///
150+
/// let mut a = BTreeSet::new();
151+
/// a.insert(1u);
152+
/// a.insert(2u);
153+
///
154+
/// let mut b = BTreeSet::new();
155+
/// b.insert(2u);
156+
/// b.insert(3u);
157+
///
158+
/// let intersection: Vec<uint> = a.intersection(&b).cloned().collect();
159+
/// assert_eq!(intersection, vec![2u]);
160+
/// ```
110161
#[unstable = "matches collection reform specification, waiting for dust to settle"]
111162
pub fn intersection<'a>(&'a self, other: &'a BTreeSet<T>)
112163
-> IntersectionItems<'a, T> {
113164
IntersectionItems{a: self.iter().peekable(), b: other.iter().peekable()}
114165
}
115166

116167
/// Visits the values representing the union, in ascending order.
168+
///
169+
/// # Example
170+
///
171+
/// ```
172+
/// use std::collections::BTreeSet;
173+
///
174+
/// let mut a = BTreeSet::new();
175+
/// a.insert(1u);
176+
///
177+
/// let mut b = BTreeSet::new();
178+
/// b.insert(2u);
179+
///
180+
/// let union: Vec<uint> = a.union(&b).cloned().collect();
181+
/// assert_eq!(union, vec![1u,2]);
182+
/// ```
117183
#[unstable = "matches collection reform specification, waiting for dust to settle"]
118184
pub fn union<'a>(&'a self, other: &'a BTreeSet<T>) -> UnionItems<'a, T> {
119185
UnionItems{a: self.iter().peekable(), b: other.iter().peekable()}

0 commit comments

Comments
 (0)