Skip to content

Commit c4ce4c8

Browse files
committed
Cleanup HashMap documentation.
Link to mentioned methods. Use `# Failure` tags to describe failure. Make `pop_equiv`, `find_equiv` and `get_copy` standalone.
1 parent 7028b3f commit c4ce4c8

File tree

1 file changed

+68
-53
lines changed

1 file changed

+68
-53
lines changed

src/libstd/collections/hashmap.rs

+68-53
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,7 @@ impl DefaultResizePolicy {
731731
/// ```
732732
///
733733
/// The easiest way to use `HashMap` with a custom type is to derive `Eq` and `Hash`.
734-
/// We must also derive `PartialEq`, but this will in the future be implied by `Eq`.
734+
/// We must also derive `PartialEq`.
735735
///
736736
/// ```
737737
/// use std::collections::HashMap;
@@ -1056,7 +1056,7 @@ impl<K: Hash + Eq, V> HashMap<K, V, RandomSipHasher> {
10561056
///
10571057
/// ```
10581058
/// use std::collections::HashMap;
1059-
/// let mut map: HashMap<&str, int> = HashMap::with_capacity(10u);
1059+
/// let mut map: HashMap<&str, int> = HashMap::with_capacity(10);
10601060
/// ```
10611061
#[inline]
10621062
pub fn with_capacity(capacity: uint) -> HashMap<K, V, RandomSipHasher> {
@@ -1100,7 +1100,7 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
11001100
/// use std::hash::sip::SipHasher;
11011101
///
11021102
/// let h = SipHasher::new();
1103-
/// let mut map = HashMap::with_capacity_and_hasher(10u, h);
1103+
/// let mut map = HashMap::with_capacity_and_hasher(10, h);
11041104
/// map.insert(1i, 2u);
11051105
/// ```
11061106
#[inline]
@@ -1123,7 +1123,7 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
11231123
/// ```
11241124
/// use std::collections::HashMap;
11251125
/// let mut map: HashMap<&str, int> = HashMap::new();
1126-
/// map.reserve(10u);
1126+
/// map.reserve(10);
11271127
/// ```
11281128
pub fn reserve(&mut self, new_minimum_capacity: uint) {
11291129
let cap = num::next_power_of_two(
@@ -1297,10 +1297,10 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
12971297
/// let mut map = HashMap::new();
12981298
///
12991299
/// // Insert 1i with key "a"
1300-
/// assert_eq!(*map.find_or_insert("a", 1i), 1i);
1300+
/// assert_eq!(*map.find_or_insert("a", 1i), 1);
13011301
///
13021302
/// // Find the existing key
1303-
/// assert_eq!(*map.find_or_insert("a", -2i), 1i);
1303+
/// assert_eq!(*map.find_or_insert("a", -2), 1);
13041304
/// ```
13051305
pub fn find_or_insert<'a>(&'a mut self, k: K, v: V) -> &'a mut V {
13061306
self.find_with_or_insert_with(k, v, |_k, _v, _a| (), |_k, a| a)
@@ -1315,11 +1315,11 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
13151315
/// use std::collections::HashMap;
13161316
/// let mut map = HashMap::new();
13171317
///
1318-
/// // Insert 10u with key 2i
1319-
/// assert_eq!(*map.find_or_insert_with(2i, |&key| { 5 * key as uint }), 10u);
1318+
/// // Insert 10 with key 2
1319+
/// assert_eq!(*map.find_or_insert_with(2i, |&key| 5 * key as uint), 10u);
13201320
///
13211321
/// // Find the existing key
1322-
/// assert_eq!(*map.find_or_insert_with(2i, |&key| { key as uint }), 10u);
1322+
/// assert_eq!(*map.find_or_insert_with(2, |&key| key as uint), 10);
13231323
/// ```
13241324
pub fn find_or_insert_with<'a>(&'a mut self, k: K, f: |&K| -> V)
13251325
-> &'a mut V {
@@ -1336,12 +1336,12 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
13361336
/// use std::collections::HashMap;
13371337
/// let mut map = HashMap::new();
13381338
///
1339-
/// // Insert 2u with key "a"
1340-
/// assert_eq!(*map.insert_or_update_with("a", 2u, |key, val| { *val = 3u; }), 2u);
1339+
/// // Insert 2 with key "a"
1340+
/// assert_eq!(*map.insert_or_update_with("a", 2u, |_key, val| *val = 3), 2);
13411341
///
13421342
/// // Update and return the existing value
1343-
/// assert_eq!(*map.insert_or_update_with("a", 9u, |key, val| { *val = 7u; }), 7u);
1344-
/// assert_eq!(map.get(&"a"), &7u);
1343+
/// assert_eq!(*map.insert_or_update_with("a", 9, |_key, val| *val = 7), 7);
1344+
/// assert_eq!(map.get(&"a"), &7);
13451345
/// ```
13461346
pub fn insert_or_update_with<'a>(
13471347
&'a mut self,
@@ -1356,9 +1356,11 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
13561356
/// insert and return a new value if it doesn't exist.
13571357
///
13581358
/// This method allows for all insertion behaviours of a hashmap;
1359-
/// see methods like `insert`, `find_or_insert` and
1360-
/// `insert_or_update_with` for less general and more friendly
1361-
/// variations of this.
1359+
/// see methods like
1360+
/// [`insert`](../trait.MutableMap.html#tymethod.insert),
1361+
/// [`find_or_insert`](#method.find_or_insert) and
1362+
/// [`insert_or_update_with`](#method.insert_or_update_with)
1363+
/// for less general and more friendly variations of this.
13621364
///
13631365
/// # Example
13641366
///
@@ -1414,8 +1416,12 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
14141416
}
14151417
}
14161418

1417-
/// Retrieves a value for the given key, failing if the key is not present.
1418-
/// See `find` for a non-failing alternative.
1419+
/// Retrieves a value for the given key.
1420+
/// See [`find`](../trait.Map.html#tymethod.find) for a non-failing alternative.
1421+
///
1422+
/// # Failure
1423+
///
1424+
/// Fails if the key is not present.
14191425
///
14201426
/// # Example
14211427
///
@@ -1424,7 +1430,7 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
14241430
///
14251431
/// let mut map = HashMap::new();
14261432
/// map.insert("a", 1i);
1427-
/// assert_eq!(map.get(&"a"), &1i);
1433+
/// assert_eq!(map.get(&"a"), &1);
14281434
/// ```
14291435
pub fn get<'a>(&'a self, k: &K) -> &'a V {
14301436
match self.find(k) {
@@ -1433,8 +1439,12 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
14331439
}
14341440
}
14351441

1436-
/// Retrieves a (mutable) value for the given key, failing if the key is not present.
1437-
/// See `find_mut` for a non-failing alternative.
1442+
/// Retrieves a mutable value for the given key.
1443+
/// See [`find_mut`](../trait.MutableMap.html#tymethod.find_mut) for a non-failing alternative.
1444+
///
1445+
/// # Failure
1446+
///
1447+
/// Fails if the key is not present.
14381448
///
14391449
/// # Example
14401450
///
@@ -1444,15 +1454,15 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
14441454
/// let mut map = HashMap::new();
14451455
/// map.insert("a", 1i);
14461456
/// {
1447-
/// // val will freeze map to prevent usage during it's lifetime
1457+
/// // val will freeze map to prevent usage during its lifetime
14481458
/// let val = map.get_mut(&"a");
1449-
/// *val = 40i;
1459+
/// *val = 40;
14501460
/// }
1451-
/// assert_eq!(map.get(&"a"), &40i);
1461+
/// assert_eq!(map.get(&"a"), &40);
14521462
///
14531463
/// // A more direct way could be:
1454-
/// *map.get_mut(&"a") = -2i;
1455-
/// assert_eq!(map.get(&"a"), &-2i);
1464+
/// *map.get_mut(&"a") = -2;
1465+
/// assert_eq!(map.get(&"a"), &-2);
14561466
/// ```
14571467
pub fn get_mut<'a>(&'a mut self, k: &K) -> &'a mut V {
14581468
match self.find_mut(k) {
@@ -1483,12 +1493,13 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
14831493
}
14841494
}
14851495

1486-
/// Like `pop`, but can operate on any type that is equivalent to a key.
1496+
/// Remove an equivalent key from the map, returning the value at the
1497+
/// key if the key was previously in the map.
14871498
///
14881499
/// # Example
14891500
///
14901501
/// This is a slightly silly example where we define the number's parity as
1491-
/// the equivilance class. It is important that the values hash the same,
1502+
/// the equivalence class. It is important that the values hash the same,
14921503
/// which is why we override `Hash`.
14931504
///
14941505
/// ```
@@ -1515,16 +1526,16 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
15151526
/// }
15161527
///
15171528
/// let mut map = HashMap::new();
1518-
/// map.insert(EvenOrOdd { num: 3u }, "foo");
1529+
/// map.insert(EvenOrOdd { num: 3 }, "foo");
15191530
///
1520-
/// assert!(map.contains_key_equiv(&EvenOrOdd { num: 1u }));
1521-
/// assert!(!map.contains_key_equiv(&EvenOrOdd { num: 4u }));
1531+
/// assert!(map.contains_key_equiv(&EvenOrOdd { num: 1 }));
1532+
/// assert!(!map.contains_key_equiv(&EvenOrOdd { num: 4 }));
15221533
///
1523-
/// assert_eq!(map.find_equiv(&EvenOrOdd { num: 5u }), Some(&"foo"));
1524-
/// assert_eq!(map.find_equiv(&EvenOrOdd { num: 2u }), None);
1534+
/// assert_eq!(map.find_equiv(&EvenOrOdd { num: 5 }), Some(&"foo"));
1535+
/// assert_eq!(map.find_equiv(&EvenOrOdd { num: 2 }), None);
15251536
///
1526-
/// assert_eq!(map.pop_equiv(&EvenOrOdd { num: 1u }), Some("foo"));
1527-
/// assert_eq!(map.pop_equiv(&EvenOrOdd { num: 2u }), None);
1537+
/// assert_eq!(map.pop_equiv(&EvenOrOdd { num: 1 }), Some("foo"));
1538+
/// assert_eq!(map.pop_equiv(&EvenOrOdd { num: 2 }), None);
15281539
///
15291540
/// ```
15301541
#[experimental]
@@ -1545,7 +1556,7 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
15451556
}
15461557

15471558
/// An iterator visiting all keys in arbitrary order.
1548-
/// Iterator element type is &'a K.
1559+
/// Iterator element type is `&'a K`.
15491560
///
15501561
/// # Example
15511562
///
@@ -1554,8 +1565,8 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
15541565
///
15551566
/// let mut map = HashMap::new();
15561567
/// map.insert("a", 1i);
1557-
/// map.insert("b", 2i);
1558-
/// map.insert("c", 3i);
1568+
/// map.insert("b", 2);
1569+
/// map.insert("c", 3);
15591570
///
15601571
/// for key in map.keys() {
15611572
/// println!("{}", key);
@@ -1566,7 +1577,7 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
15661577
}
15671578

15681579
/// An iterator visiting all values in arbitrary order.
1569-
/// Iterator element type is &'a V.
1580+
/// Iterator element type is `&'a V`.
15701581
///
15711582
/// # Example
15721583
///
@@ -1575,8 +1586,8 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
15751586
///
15761587
/// let mut map = HashMap::new();
15771588
/// map.insert("a", 1i);
1578-
/// map.insert("b", 2i);
1579-
/// map.insert("c", 3i);
1589+
/// map.insert("b", 2);
1590+
/// map.insert("c", 3);
15801591
///
15811592
/// for key in map.values() {
15821593
/// println!("{}", key);
@@ -1587,7 +1598,7 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
15871598
}
15881599

15891600
/// An iterator visiting all key-value pairs in arbitrary order.
1590-
/// Iterator element type is (&'a K, &'a V).
1601+
/// Iterator element type is `(&'a K, &'a V)`.
15911602
///
15921603
/// # Example
15931604
///
@@ -1596,8 +1607,8 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
15961607
///
15971608
/// let mut map = HashMap::new();
15981609
/// map.insert("a", 1i);
1599-
/// map.insert("b", 2i);
1600-
/// map.insert("c", 3i);
1610+
/// map.insert("b", 2);
1611+
/// map.insert("c", 3);
16011612
///
16021613
/// for (key, val) in map.iter() {
16031614
/// println!("key: {} val: {}", key, val);
@@ -1609,7 +1620,7 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
16091620

16101621
/// An iterator visiting all key-value pairs in arbitrary order,
16111622
/// with mutable references to the values.
1612-
/// Iterator element type is (&'a K, &'a mut V).
1623+
/// Iterator element type is `(&'a K, &'a mut V)`.
16131624
///
16141625
/// # Example
16151626
///
@@ -1618,8 +1629,8 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
16181629
///
16191630
/// let mut map = HashMap::new();
16201631
/// map.insert("a", 1i);
1621-
/// map.insert("b", 2i);
1622-
/// map.insert("c", 3i);
1632+
/// map.insert("b", 2);
1633+
/// map.insert("c", 3);
16231634
///
16241635
/// // Update all values
16251636
/// for (_, val) in map.mut_iter() {
@@ -1645,8 +1656,8 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
16451656
///
16461657
/// let mut map = HashMap::new();
16471658
/// map.insert("a", 1i);
1648-
/// map.insert("b", 2i);
1649-
/// map.insert("c", 3i);
1659+
/// map.insert("b", 2);
1660+
/// map.insert("c", 3);
16501661
///
16511662
/// // Not possible with .iter()
16521663
/// let vec: Vec<(&str, int)> = map.move_iter().collect();
@@ -1657,7 +1668,7 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
16571668
}
16581669

16591670
impl<K: Eq + Hash<S>, V: Clone, S, H: Hasher<S>> HashMap<K, V, H> {
1660-
/// Like `find`, but returns a copy of the value.
1671+
/// Return a copy of the value corresponding to the key.
16611672
///
16621673
/// # Example
16631674
///
@@ -1666,13 +1677,17 @@ impl<K: Eq + Hash<S>, V: Clone, S, H: Hasher<S>> HashMap<K, V, H> {
16661677
///
16671678
/// let mut map: HashMap<uint, String> = HashMap::new();
16681679
/// map.insert(1u, "foo".to_string());
1669-
/// let s: String = map.find_copy(&1u).unwrap();
1680+
/// let s: String = map.find_copy(&1).unwrap();
16701681
/// ```
16711682
pub fn find_copy(&self, k: &K) -> Option<V> {
16721683
self.find(k).map(|v| (*v).clone())
16731684
}
16741685

1675-
/// Like `get`, but returns a copy of the value.
1686+
/// Return a copy of the value corresponding to the key.
1687+
///
1688+
/// # Failure
1689+
///
1690+
/// Fails if the key is not present.
16761691
///
16771692
/// # Example
16781693
///
@@ -1681,7 +1696,7 @@ impl<K: Eq + Hash<S>, V: Clone, S, H: Hasher<S>> HashMap<K, V, H> {
16811696
///
16821697
/// let mut map: HashMap<uint, String> = HashMap::new();
16831698
/// map.insert(1u, "foo".to_string());
1684-
/// let s: String = map.get_copy(&1u);
1699+
/// let s: String = map.get_copy(&1);
16851700
/// ```
16861701
pub fn get_copy(&self, k: &K) -> V {
16871702
(*self.get(k)).clone()

0 commit comments

Comments
 (0)