|
12 | 12 | //!
|
13 | 13 | //! The tables use a keyed hash with new random keys generated for each container, so the ordering
|
14 | 14 | //! of a set of keys in a hash table is randomized.
|
| 15 | +//! |
| 16 | +//! # Example |
| 17 | +//! |
| 18 | +//! ```rust |
| 19 | +//! use std::hashmap::HashMap; |
| 20 | +//! |
| 21 | +//! // type inference lets us omit an explicit type signature (which |
| 22 | +//! // would be `HashMap<&str, &str>` in this example). |
| 23 | +//! let mut book_reviews = HashMap::new(); |
| 24 | +//! |
| 25 | +//! // review some books. |
| 26 | +//! book_reviews.insert("Adventures of Hucklebury Fin", "My favorite book."); |
| 27 | +//! book_reviews.insert("Grimms' Fairy Tales", "Masterpiece."); |
| 28 | +//! book_reviews.insert("Pride and Prejudice", "Very enjoyable."); |
| 29 | +//! book_reviews.insert("The Adventures of Sherlock Holmes", "Eye lyked it alot."); |
| 30 | +//! |
| 31 | +//! // check for a specific one. |
| 32 | +//! if !book_reviews.contains_key(& &"Les Misérables") { |
| 33 | +//! println!("We've got {} reviews, but Les Misérables ain't one.", |
| 34 | +//! book_reviews.len()); |
| 35 | +//! } |
| 36 | +//! |
| 37 | +//! // oops, this review has a lot of spelling mistakes, let's delete it. |
| 38 | +//! book_reviews.remove(& &"The Adventures of Sherlock Holmes"); |
| 39 | +//! |
| 40 | +//! // look up the values associated with some keys. |
| 41 | +//! let to_find = ["Pride and Prejudice", "Alice's Adventure in Wonderland"]; |
| 42 | +//! for book in to_find.iter() { |
| 43 | +//! match book_reviews.find(book) { |
| 44 | +//! Some(review) => println!("{}: {}", *book, *review), |
| 45 | +//! None => println!("{} is unreviewed.", *book) |
| 46 | +//! } |
| 47 | +//! } |
| 48 | +//! |
| 49 | +//! // iterate over everything. |
| 50 | +//! for (book, review) in book_reviews.iter() { |
| 51 | +//! println!("{}: \"{}\"", *book, *review); |
| 52 | +//! } |
| 53 | +//! ``` |
15 | 54 |
|
16 | 55 | use container::{Container, Mutable, Map, MutableMap, Set, MutableSet};
|
17 | 56 | use clone::Clone;
|
@@ -354,6 +393,43 @@ impl<K: Hash + Eq, V> HashMap<K, V> {
|
354 | 393 |
|
355 | 394 | /// Modify and return the value corresponding to the key in the map, or
|
356 | 395 | /// insert and return a new value if it doesn't exist.
|
| 396 | + /// |
| 397 | + /// This method allows for all insertion behaviours of a hashmap, |
| 398 | + /// see methods like `insert`, `find_or_insert` and |
| 399 | + /// `insert_or_update_with` for less general and more friendly |
| 400 | + /// variations of this. |
| 401 | + /// |
| 402 | + /// # Example |
| 403 | + /// |
| 404 | + /// ```rust |
| 405 | + /// use std::hashmap::HashMap; |
| 406 | + /// |
| 407 | + /// // map some strings to vectors of strings |
| 408 | + /// let mut map = HashMap::<~str, ~[~str]>::new(); |
| 409 | + /// map.insert(~"a key", ~[~"value"]); |
| 410 | + /// map.insert(~"z key", ~[~"value"]); |
| 411 | + /// |
| 412 | + /// let new = ~[~"a key", ~"b key", ~"z key"]; |
| 413 | + /// for k in new.move_iter() { |
| 414 | + /// map.mangle(k, ~"new value", |
| 415 | + /// // if the key doesn't exist in the map yet, add it in |
| 416 | + /// // the obvious way. |
| 417 | + /// |_k, v| ~[v], |
| 418 | + /// // if the key does exist either prepend or append this |
| 419 | + /// // new value based on the first letter of the key. |
| 420 | + /// |key, already, new| { |
| 421 | + /// if key.starts_with("z") { |
| 422 | + /// already.unshift(new); |
| 423 | + /// } else { |
| 424 | + /// already.push(new); |
| 425 | + /// } |
| 426 | + /// }); |
| 427 | + /// } |
| 428 | + /// |
| 429 | + /// for (k, v) in map.iter() { |
| 430 | + /// println!("{} -> {:?}", *k, *v); |
| 431 | + /// } |
| 432 | + /// ``` |
357 | 433 | pub fn mangle<'a,
|
358 | 434 | A>(
|
359 | 435 | &'a mut self,
|
|
0 commit comments