@@ -1484,6 +1484,36 @@ pub type SetMoveItems<K> =
1484
1484
/// An implementation of a hash set using the underlying representation of a
1485
1485
/// HashMap where the value is (). As with the `HashMap` type, a `HashSet`
1486
1486
/// 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
+ /// ```
1487
1517
#[ deriving( Clone ) ]
1488
1518
pub struct HashSet < T , H = RandomSipHasher > {
1489
1519
map : HashMap < T , ( ) , H >
@@ -1527,13 +1557,27 @@ impl<T: Eq + Hash<S>, S, H: Hasher<S>> MutableSet<T> for HashSet<T, H> {
1527
1557
1528
1558
impl < T : Hash + Eq > HashSet < T , RandomSipHasher > {
1529
1559
/// Create an empty HashSet
1560
+ ///
1561
+ /// # Example
1562
+ ///
1563
+ /// ```rust
1564
+ /// # use std::collections::HashSet;
1565
+ /// let mut set: HashSet<int> = HashSet::new();
1566
+ /// ```
1530
1567
#[ inline]
1531
1568
pub fn new ( ) -> HashSet < T , RandomSipHasher > {
1532
1569
HashSet :: with_capacity ( INITIAL_CAPACITY )
1533
1570
}
1534
1571
1535
1572
/// Create an empty HashSet with space for at least `n` elements in
1536
1573
/// 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
+ /// ```
1537
1581
#[ inline]
1538
1582
pub fn with_capacity ( capacity : uint ) -> HashSet < T , RandomSipHasher > {
1539
1583
HashSet { map : HashMap :: with_capacity ( capacity) }
@@ -1563,6 +1607,14 @@ impl<T: Eq + Hash<S>, S, H: Hasher<S>> HashSet<T, H> {
1563
1607
}
1564
1608
1565
1609
/// 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
+ /// ```
1566
1618
pub fn reserve ( & mut self , n : uint ) {
1567
1619
self . map . reserve ( n)
1568
1620
}
@@ -1575,32 +1627,119 @@ impl<T: Eq + Hash<S>, S, H: Hasher<S>> HashSet<T, H> {
1575
1627
1576
1628
/// An iterator visiting all elements in arbitrary order.
1577
1629
/// 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
+ /// ```
1578
1644
pub fn iter < ' a > ( & ' a self ) -> SetItems < ' a , T > {
1579
1645
self . map . keys ( )
1580
1646
}
1581
1647
1582
1648
/// Creates a consuming iterator, that is, one that moves each value out
1583
1649
/// of the set in arbitrary order. The set cannot be used after calling
1584
1650
/// 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
+ /// ```
1585
1668
pub fn move_iter ( self ) -> SetMoveItems < T > {
1586
1669
self . map . move_iter ( ) . map ( |( k, _) | k)
1587
1670
}
1588
1671
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
+ /// ```
1590
1694
pub fn difference < ' a > ( & ' a self , other : & ' a HashSet < T , H > ) -> SetAlgebraItems < ' a , T , H > {
1591
1695
Repeat :: new ( other) . zip ( self . iter ( ) )
1592
1696
. filter_map ( |( other, elt) | {
1593
1697
if !other. contains ( elt) { Some ( elt) } else { None }
1594
1698
} )
1595
1699
}
1596
1700
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
+ /// ```
1598
1721
pub fn symmetric_difference < ' a > ( & ' a self , other : & ' a HashSet < T , H > )
1599
1722
-> Chain < SetAlgebraItems < ' a , T , H > , SetAlgebraItems < ' a , T , H > > {
1600
1723
self . difference ( other) . chain ( other. difference ( self ) )
1601
1724
}
1602
1725
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
+ /// ```
1604
1743
pub fn intersection < ' a > ( & ' a self , other : & ' a HashSet < T , H > )
1605
1744
-> SetAlgebraItems < ' a , T , H > {
1606
1745
Repeat :: new ( other) . zip ( self . iter ( ) )
@@ -1609,7 +1748,23 @@ impl<T: Eq + Hash<S>, S, H: Hasher<S>> HashSet<T, H> {
1609
1748
} )
1610
1749
}
1611
1750
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
+ /// ```
1613
1768
pub fn union < ' a > ( & ' a self , other : & ' a HashSet < T , H > )
1614
1769
-> Chain < SetItems < ' a , T > , SetAlgebraItems < ' a , T , H > > {
1615
1770
self . iter ( ) . chain ( other. difference ( self ) )
0 commit comments