Skip to content

Commit dac677a

Browse files
committed
Rollup merge of #25074 - killercup:patch-10, r=alexcrichton
Sweeten the two main HashMap/HashSet examples from [here](http://doc.rust-lang.org/std/collections/struct.HashMap.html) and [here](http://doc.rust-lang.org/std/collections/struct.HashSet.html) with some deref and loop sugar. (I've only tested this using [this playpen][1].) [1]: https://play.rust-lang.org/?code=fn%20main()%20%7B%0A%20%20%20%20use%20std%3A%3Acollections%3A%3AHashMap%3B%0A%20%20%20%20%0A%20%20%20%20%2F%2F%20type%20inference%20lets%20us%20omit%20an%20explicit%20type%20signature%20(which%0A%20%20%20%20%2F%2F%20would%20be%20%60HashMap%3C%26str%2C%20%26str%3E%60%20in%20this%20example).%0A%20%20%20%20let%20mut%20book_reviews%20%3D%20HashMap%3A%3Anew()%3B%0A%20%20%20%20%0A%20%20%20%20%2F%2F%20review%20some%20books.%0A%20%20%20%20book_reviews.insert(%22Adventures%20of%20Huckleberry%20Finn%22%2C%20%20%20%20%22My%20favorite%20book.%22)%3B%0A%20%20%20%20book_reviews.insert(%22Grimms%27%20Fairy%20Tales%22%2C%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Masterpiece.%22)%3B%0A%20%20%20%20book_reviews.insert(%22Pride%20and%20Prejudice%22%2C%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Very%20enjoyable.%22)%3B%0A%20%20%20%20book_reviews.insert(%22The%20Adventures%20of%20Sherlock%20Holmes%22%2C%20%22Eye%20lyked%20it%20alot.%22)%3B%0A%20%20%20%20%0A%20%20%20%20%2F%2F%20check%20for%20a%20specific%20one.%0A%20%20%20%20if%20!book_reviews.contains_key(%26(%22Les%20Mis%C3%A9rables%22))%20%7B%0A%20%20%20%20%20%20%20%20println!(%22We%27ve%20got%20%7B%7D%20reviews%2C%20but%20Les%20Mis%C3%A9rables%20ain%27t%20one.%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20book_reviews.len())%3B%0A%20%20%20%20%7D%0A%20%20%20%20%0A%20%20%20%20%2F%2F%20oops%2C%20this%20review%20has%20a%20lot%20of%20spelling%20mistakes%2C%20let%27s%20delete%20it.%0A%20%20%20%20book_reviews.remove(%26(%22The%20Adventures%20of%20Sherlock%20Holmes%22))%3B%0A%20%20%20%20%0A%20%20%20%20%2F%2F%20look%20up%20the%20values%20associated%20with%20some%20keys.%0A%20%20%20%20let%20to_find%20%3D%20%5B%22Pride%20and%20Prejudice%22%2C%20%22Alice%27s%20Adventure%20in%20Wonderland%22%5D%3B%0A%20%20%20%20for%20book%20in%20to_find.iter()%20%7B%0A%20%20%20%20%20%20%20%20match%20book_reviews.get(book)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20Some(review)%20%3D%3E%20println!(%22%7B%7D%3A%20%7B%7D%22%2C%20*book%2C%20*review)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20None%20%3D%3E%20println!(%22%7B%7D%20is%20unreviewed.%22%2C%20*book)%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%20%20%20%0A%20%20%20%20%2F%2F%20iterate%20over%20everything.%0A%20%20%20%20for%20(book%2C%20review)%20in%20book_reviews.iter()%20%7B%0A%20%20%20%20%20%20%20%20println!(%22%7B%7D%3A%20%5C%22%7B%7D%5C%22%22%2C%20*book%2C%20*review)%3B%0A%20%20%20%20%7D%0A%20%20%20%20%0A%7D
2 parents d0fefa1 + 2ac380a commit dac677a

File tree

2 files changed

+22
-19
lines changed

2 files changed

+22
-19
lines changed

src/libstd/collections/hash/map.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,9 @@ fn test_resize_policy() {
212212
/// overridden with one of the constructors.
213213
///
214214
/// It is required that the keys implement the `Eq` and `Hash` traits, although
215-
/// this can frequently be achieved by using `#[derive(Eq, Hash)]`. If you
216-
/// implement these yourself, it is important that the following property holds:
215+
/// this can frequently be achieved by using `#[derive(PartialEq, Eq, Hash)]`.
216+
/// If you implement these yourself, it is important that the following
217+
/// property holds:
217218
///
218219
/// ```text
219220
/// k1 == k2 -> hash(k1) == hash(k2)
@@ -250,26 +251,26 @@ fn test_resize_policy() {
250251
/// book_reviews.insert("The Adventures of Sherlock Holmes", "Eye lyked it alot.");
251252
///
252253
/// // check for a specific one.
253-
/// if !book_reviews.contains_key(&("Les Misérables")) {
254+
/// if !book_reviews.contains_key("Les Misérables") {
254255
/// println!("We've got {} reviews, but Les Misérables ain't one.",
255256
/// book_reviews.len());
256257
/// }
257258
///
258259
/// // oops, this review has a lot of spelling mistakes, let's delete it.
259-
/// book_reviews.remove(&("The Adventures of Sherlock Holmes"));
260+
/// book_reviews.remove("The Adventures of Sherlock Holmes");
260261
///
261262
/// // look up the values associated with some keys.
262263
/// let to_find = ["Pride and Prejudice", "Alice's Adventure in Wonderland"];
263-
/// for book in to_find.iter() {
264+
/// for book in &to_find {
264265
/// match book_reviews.get(book) {
265-
/// Some(review) => println!("{}: {}", *book, *review),
266-
/// None => println!("{} is unreviewed.", *book)
266+
/// Some(review) => println!("{}: {}", book, review),
267+
/// None => println!("{} is unreviewed.", book)
267268
/// }
268269
/// }
269270
///
270271
/// // iterate over everything.
271-
/// for (book, review) in book_reviews.iter() {
272-
/// println!("{}: \"{}\"", *book, *review);
272+
/// for (book, review) in &book_reviews {
273+
/// println!("{}: \"{}\"", book, review);
273274
/// }
274275
/// ```
275276
///
@@ -300,7 +301,7 @@ fn test_resize_policy() {
300301
/// vikings.insert(Viking::new("Harald", "Iceland"), 12);
301302
///
302303
/// // Use derived implementation to print the status of the vikings.
303-
/// for (viking, health) in vikings.iter() {
304+
/// for (viking, health) in &vikings {
304305
/// println!("{:?} has {} hp", viking, health);
305306
/// }
306307
/// ```

src/libstd/collections/hash/set.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@ use super::state::HashState;
3131
// to get rid of it properly.
3232

3333
/// An implementation of a hash set using the underlying representation of a
34-
/// HashMap where the value is (). As with the `HashMap` type, a `HashSet`
35-
/// requires that the elements implement the `Eq` and `Hash` traits. This can
36-
/// frequently be achieved by using `#[derive(Eq, Hash)]`. If you implement
37-
/// these yourself, it is important that the following property holds:
34+
/// HashMap where the value is ().
35+
///
36+
/// As with the `HashMap` type, a `HashSet` requires that the elements
37+
/// implement the `Eq` and `Hash` traits. This can frequently be achieved by
38+
/// using `#[derive(PartialEq, Eq, Hash)]`. If you implement these yourself,
39+
/// it is important that the following property holds:
3840
///
3941
/// ```text
4042
/// k1 == k2 -> hash(k1) == hash(k2)
@@ -64,17 +66,17 @@ use super::state::HashState;
6466
/// books.insert("The Great Gatsby");
6567
///
6668
/// // Check for a specific one.
67-
/// if !books.contains(&("The Winds of Winter")) {
69+
/// if !books.contains("The Winds of Winter") {
6870
/// println!("We have {} books, but The Winds of Winter ain't one.",
6971
/// books.len());
7072
/// }
7173
///
7274
/// // Remove a book.
73-
/// books.remove(&"The Odyssey");
75+
/// books.remove("The Odyssey");
7476
///
7577
/// // Iterate over everything.
76-
/// for book in books.iter() {
77-
/// println!("{}", *book);
78+
/// for book in &books {
79+
/// println!("{}", book);
7880
/// }
7981
/// ```
8082
///
@@ -98,7 +100,7 @@ use super::state::HashState;
98100
/// vikings.insert(Viking { name: "Harald", power: 8 });
99101
///
100102
/// // Use derived implementation to print the vikings.
101-
/// for x in vikings.iter() {
103+
/// for x in &vikings {
102104
/// println!("{:?}", x);
103105
/// }
104106
/// ```

0 commit comments

Comments
 (0)