Skip to content

Commit 06c7ee9

Browse files
committed
auto merge of #15667 : treeman/rust/set-doc, r=alexcrichton
Example code for HashSet, similar to the [HashMap example](http://doc.rust-lang.org/std/collections/hashmap/struct.HashMap.html).
2 parents 175f113 + 80ef6b8 commit 06c7ee9

File tree

1 file changed

+159
-4
lines changed

1 file changed

+159
-4
lines changed

Diff for: src/libstd/collections/hashmap.rs

+159-4
Original file line numberDiff line numberDiff line change
@@ -1484,6 +1484,36 @@ pub type SetMoveItems<K> =
14841484
/// An implementation of a hash set using the underlying representation of a
14851485
/// HashMap where the value is (). As with the `HashMap` type, a `HashSet`
14861486
/// requires that the elements implement the `Eq` and `Hash` traits.
1487+
///
1488+
/// # Example
1489+
///
1490+
/// ```rust
1491+
/// use std::collections::HashSet;
1492+
///
1493+
/// // Type inference lets us omit an explicit type signature (which
1494+
/// // would be `HashSet<&str>` in this example).
1495+
/// let mut books = HashSet::new();
1496+
///
1497+
/// // Add some books.
1498+
/// books.insert("A Dance With Dragons");
1499+
/// books.insert("To Kill a Mockingbird");
1500+
/// books.insert("The Odyssey");
1501+
/// books.insert("The Great Gatsby");
1502+
///
1503+
/// // Check for a specific one.
1504+
/// if !books.contains(&("The Winds of Winter")) {
1505+
/// println!("We have {} books, but The Winds of Winter ain't one.",
1506+
/// books.len());
1507+
/// }
1508+
///
1509+
/// // Remove a book.
1510+
/// books.remove(&"The Odyssey");
1511+
///
1512+
/// // Iterate over everything.
1513+
/// for book in books.iter() {
1514+
/// println!("{}", *book);
1515+
/// }
1516+
/// ```
14871517
#[deriving(Clone)]
14881518
pub struct HashSet<T, H = RandomSipHasher> {
14891519
map: HashMap<T, (), H>
@@ -1527,13 +1557,27 @@ impl<T: Eq + Hash<S>, S, H: Hasher<S>> MutableSet<T> for HashSet<T, H> {
15271557

15281558
impl<T: Hash + Eq> HashSet<T, RandomSipHasher> {
15291559
/// Create an empty HashSet
1560+
///
1561+
/// # Example
1562+
///
1563+
/// ```rust
1564+
/// # use std::collections::HashSet;
1565+
/// let mut set: HashSet<int> = HashSet::new();
1566+
/// ```
15301567
#[inline]
15311568
pub fn new() -> HashSet<T, RandomSipHasher> {
15321569
HashSet::with_capacity(INITIAL_CAPACITY)
15331570
}
15341571

15351572
/// Create an empty HashSet with space for at least `n` elements in
15361573
/// the hash table.
1574+
///
1575+
/// # Example
1576+
///
1577+
/// ```rust
1578+
/// # use std::collections::HashSet;
1579+
/// let mut set: HashSet<int> = HashSet::with_capacity(10);
1580+
/// ```
15371581
#[inline]
15381582
pub fn with_capacity(capacity: uint) -> HashSet<T, RandomSipHasher> {
15391583
HashSet { map: HashMap::with_capacity(capacity) }
@@ -1563,6 +1607,14 @@ impl<T: Eq + Hash<S>, S, H: Hasher<S>> HashSet<T, H> {
15631607
}
15641608

15651609
/// Reserve space for at least `n` elements in the hash table.
1610+
///
1611+
/// # Example
1612+
///
1613+
/// ```rust
1614+
/// # use std::collections::HashSet;
1615+
/// let mut set: HashSet<int> = HashSet::new();
1616+
/// set.reserve(10);
1617+
/// ```
15661618
pub fn reserve(&mut self, n: uint) {
15671619
self.map.reserve(n)
15681620
}
@@ -1575,32 +1627,119 @@ impl<T: Eq + Hash<S>, S, H: Hasher<S>> HashSet<T, H> {
15751627

15761628
/// An iterator visiting all elements in arbitrary order.
15771629
/// Iterator element type is &'a T.
1630+
///
1631+
/// # Example
1632+
///
1633+
/// ```rust
1634+
/// # use std::collections::HashSet;
1635+
/// let mut set = HashSet::new();
1636+
/// set.insert("a");
1637+
/// set.insert("b");
1638+
///
1639+
/// // Will print in an arbitrary order.
1640+
/// for x in set.iter() {
1641+
/// println!("{}", x);
1642+
/// }
1643+
/// ```
15781644
pub fn iter<'a>(&'a self) -> SetItems<'a, T> {
15791645
self.map.keys()
15801646
}
15811647

15821648
/// Creates a consuming iterator, that is, one that moves each value out
15831649
/// of the set in arbitrary order. The set cannot be used after calling
15841650
/// this.
1651+
///
1652+
/// # Example
1653+
///
1654+
/// ```rust
1655+
/// # use std::collections::HashSet;
1656+
/// let mut set = HashSet::new();
1657+
/// set.insert("a".to_string());
1658+
/// set.insert("b".to_string());
1659+
///
1660+
/// // Not possible to collect to a Vec<String> with a regular `.iter()`.
1661+
/// let v: Vec<String> = set.move_iter().collect();
1662+
///
1663+
/// // Will print in an arbitrary order.
1664+
/// for x in v.iter() {
1665+
/// println!("{}", x);
1666+
/// }
1667+
/// ```
15851668
pub fn move_iter(self) -> SetMoveItems<T> {
15861669
self.map.move_iter().map(|(k, _)| k)
15871670
}
15881671

1589-
/// Visit the values representing the difference
1672+
/// Visit the values representing the difference.
1673+
///
1674+
/// # Example
1675+
///
1676+
/// ```rust
1677+
/// # use std::collections::HashSet;
1678+
/// let a: HashSet<int> = [1i, 2, 3].iter().map(|&x| x).collect();
1679+
/// let b: HashSet<int> = [4i, 2, 3, 4].iter().map(|&x| x).collect();
1680+
///
1681+
/// // Can be seen as `a - b`.
1682+
/// for x in a.difference(&b) {
1683+
/// println!("{}", x); // Print 1
1684+
/// }
1685+
///
1686+
/// let diff: HashSet<int> = a.difference(&b).map(|&x| x).collect();
1687+
/// assert_eq!(diff, [1i].iter().map(|&x| x).collect());
1688+
///
1689+
/// // Note that difference is not symmetric,
1690+
/// // and `b - a` means something else:
1691+
/// let diff: HashSet<int> = b.difference(&a).map(|&x| x).collect();
1692+
/// assert_eq!(diff, [4i].iter().map(|&x| x).collect());
1693+
/// ```
15901694
pub fn difference<'a>(&'a self, other: &'a HashSet<T, H>) -> SetAlgebraItems<'a, T, H> {
15911695
Repeat::new(other).zip(self.iter())
15921696
.filter_map(|(other, elt)| {
15931697
if !other.contains(elt) { Some(elt) } else { None }
15941698
})
15951699
}
15961700

1597-
/// Visit the values representing the symmetric difference
1701+
/// Visit the values representing the symmetric difference.
1702+
///
1703+
/// # Example
1704+
///
1705+
/// ```rust
1706+
/// # use std::collections::HashSet;
1707+
/// let a: HashSet<int> = [1i, 2, 3].iter().map(|&x| x).collect();
1708+
/// let b: HashSet<int> = [4i, 2, 3, 4].iter().map(|&x| x).collect();
1709+
///
1710+
/// // Print 1, 4 in arbitrary order.
1711+
/// for x in a.symmetric_difference(&b) {
1712+
/// println!("{}", x);
1713+
/// }
1714+
///
1715+
/// let diff1: HashSet<int> = a.symmetric_difference(&b).map(|&x| x).collect();
1716+
/// let diff2: HashSet<int> = b.symmetric_difference(&a).map(|&x| x).collect();
1717+
///
1718+
/// assert_eq!(diff1, diff2);
1719+
/// assert_eq!(diff1, [1i, 4].iter().map(|&x| x).collect());
1720+
/// ```
15981721
pub fn symmetric_difference<'a>(&'a self, other: &'a HashSet<T, H>)
15991722
-> Chain<SetAlgebraItems<'a, T, H>, SetAlgebraItems<'a, T, H>> {
16001723
self.difference(other).chain(other.difference(self))
16011724
}
16021725

1603-
/// Visit the values representing the intersection
1726+
/// Visit the values representing the intersection.
1727+
///
1728+
/// # Example
1729+
///
1730+
/// ```rust
1731+
/// # use std::collections::HashSet;
1732+
/// let a: HashSet<int> = [1i, 2, 3].iter().map(|&x| x).collect();
1733+
/// let b: HashSet<int> = [4i, 2, 3, 4].iter().map(|&x| x).collect();
1734+
///
1735+
/// // Print 2, 3 in arbitrary order.
1736+
/// for x in a.intersection(&b) {
1737+
/// println!("{}", x);
1738+
/// }
1739+
///
1740+
/// let diff: HashSet<int> = a.intersection(&b).map(|&x| x).collect();
1741+
/// assert_eq!(diff, [2i, 3].iter().map(|&x| x).collect());
1742+
/// ```
16041743
pub fn intersection<'a>(&'a self, other: &'a HashSet<T, H>)
16051744
-> SetAlgebraItems<'a, T, H> {
16061745
Repeat::new(other).zip(self.iter())
@@ -1609,7 +1748,23 @@ impl<T: Eq + Hash<S>, S, H: Hasher<S>> HashSet<T, H> {
16091748
})
16101749
}
16111750

1612-
/// Visit the values representing the union
1751+
/// Visit the values representing the union.
1752+
///
1753+
/// # Example
1754+
///
1755+
/// ```rust
1756+
/// # use std::collections::HashSet;
1757+
/// let a: HashSet<int> = [1i, 2, 3].iter().map(|&x| x).collect();
1758+
/// let b: HashSet<int> = [4i, 2, 3, 4].iter().map(|&x| x).collect();
1759+
///
1760+
/// // Print 1, 2, 3, 4 in arbitrary order.
1761+
/// for x in a.union(&b) {
1762+
/// println!("{}", x);
1763+
/// }
1764+
///
1765+
/// let diff: HashSet<int> = a.union(&b).map(|&x| x).collect();
1766+
/// assert_eq!(diff, [1i, 2, 3, 4].iter().map(|&x| x).collect());
1767+
/// ```
16131768
pub fn union<'a>(&'a self, other: &'a HashSet<T, H>)
16141769
-> Chain<SetItems<'a, T>, SetAlgebraItems<'a, T, H>> {
16151770
self.iter().chain(other.difference(self))

0 commit comments

Comments
 (0)