Skip to content

Commit a646c91

Browse files
committed
Auto merge of #51081 - kornelski:examplestr, r=steveklabnik
Use String, not &str in some collection examples Discussed in #46966 Overuse of borrowed values in data structures is a common mistake I see in Rust user forums. Users who copy&paste such examples end up fighting with the borrow checker as soon as they replace string literals with some real values. This changes a couple of examples to use `String`, and it adds opportunity to demonstrate use of `Borrow`.
2 parents 72b0f91 + 01e82f1 commit a646c91

File tree

2 files changed

+37
-23
lines changed

2 files changed

+37
-23
lines changed

src/libstd/collections/hash/map.rs

+25-11
Original file line numberDiff line numberDiff line change
@@ -276,17 +276,31 @@ const DISPLACEMENT_THRESHOLD: usize = 128;
276276
/// ```
277277
/// use std::collections::HashMap;
278278
///
279-
/// // type inference lets us omit an explicit type signature (which
280-
/// // would be `HashMap<&str, &str>` in this example).
279+
/// // Type inference lets us omit an explicit type signature (which
280+
/// // would be `HashMap<String, String>` in this example).
281281
/// let mut book_reviews = HashMap::new();
282282
///
283-
/// // review some books.
284-
/// book_reviews.insert("Adventures of Huckleberry Finn", "My favorite book.");
285-
/// book_reviews.insert("Grimms' Fairy Tales", "Masterpiece.");
286-
/// book_reviews.insert("Pride and Prejudice", "Very enjoyable.");
287-
/// book_reviews.insert("The Adventures of Sherlock Holmes", "Eye lyked it alot.");
283+
/// // Review some books.
284+
/// book_reviews.insert(
285+
/// "Adventures of Huckleberry Finn".to_string(),
286+
/// "My favorite book.".to_string(),
287+
/// );
288+
/// book_reviews.insert(
289+
/// "Grimms' Fairy Tales".to_string(),
290+
/// "Masterpiece.".to_string(),
291+
/// );
292+
/// book_reviews.insert(
293+
/// "Pride and Prejudice".to_string(),
294+
/// "Very enjoyable.".to_string(),
295+
/// );
296+
/// book_reviews.insert(
297+
/// "The Adventures of Sherlock Holmes".to_string(),
298+
/// "Eye lyked it alot.".to_string(),
299+
/// );
288300
///
289-
/// // check for a specific one.
301+
/// // Check for a specific one.
302+
/// // When collections store owned values (String), they can still be
303+
/// // queried using references (&str).
290304
/// if !book_reviews.contains_key("Les Misérables") {
291305
/// println!("We've got {} reviews, but Les Misérables ain't one.",
292306
/// book_reviews.len());
@@ -295,16 +309,16 @@ const DISPLACEMENT_THRESHOLD: usize = 128;
295309
/// // oops, this review has a lot of spelling mistakes, let's delete it.
296310
/// book_reviews.remove("The Adventures of Sherlock Holmes");
297311
///
298-
/// // look up the values associated with some keys.
312+
/// // Look up the values associated with some keys.
299313
/// let to_find = ["Pride and Prejudice", "Alice's Adventure in Wonderland"];
300-
/// for book in &to_find {
314+
/// for &book in &to_find {
301315
/// match book_reviews.get(book) {
302316
/// Some(review) => println!("{}: {}", book, review),
303317
/// None => println!("{} is unreviewed.", book)
304318
/// }
305319
/// }
306320
///
307-
/// // iterate over everything.
321+
/// // Iterate over everything.
308322
/// for (book, review) in &book_reviews {
309323
/// println!("{}: \"{}\"", book, review);
310324
/// }

src/libstd/collections/hash/set.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,14 @@ use super::map::{self, HashMap, Keys, RandomState};
4949
/// ```
5050
/// use std::collections::HashSet;
5151
/// // Type inference lets us omit an explicit type signature (which
52-
/// // would be `HashSet<&str>` in this example).
52+
/// // would be `HashSet<String>` in this example).
5353
/// let mut books = HashSet::new();
5454
///
5555
/// // Add some books.
56-
/// books.insert("A Dance With Dragons");
57-
/// books.insert("To Kill a Mockingbird");
58-
/// books.insert("The Odyssey");
59-
/// books.insert("The Great Gatsby");
56+
/// books.insert("A Dance With Dragons".to_string());
57+
/// books.insert("To Kill a Mockingbird".to_string());
58+
/// books.insert("The Odyssey".to_string());
59+
/// books.insert("The Great Gatsby".to_string());
6060
///
6161
/// // Check for a specific one.
6262
/// if !books.contains("The Winds of Winter") {
@@ -80,17 +80,17 @@ use super::map::{self, HashMap, Keys, RandomState};
8080
/// ```
8181
/// use std::collections::HashSet;
8282
/// #[derive(Hash, Eq, PartialEq, Debug)]
83-
/// struct Viking<'a> {
84-
/// name: &'a str,
83+
/// struct Viking {
84+
/// name: String,
8585
/// power: usize,
8686
/// }
8787
///
8888
/// let mut vikings = HashSet::new();
8989
///
90-
/// vikings.insert(Viking { name: "Einar", power: 9 });
91-
/// vikings.insert(Viking { name: "Einar", power: 9 });
92-
/// vikings.insert(Viking { name: "Olaf", power: 4 });
93-
/// vikings.insert(Viking { name: "Harald", power: 8 });
90+
/// vikings.insert(Viking { name: "Einar".to_string(), power: 9 });
91+
/// vikings.insert(Viking { name: "Einar".to_string(), power: 9 });
92+
/// vikings.insert(Viking { name: "Olaf".to_string(), power: 4 });
93+
/// vikings.insert(Viking { name: "Harald".to_string(), power: 8 });
9494
///
9595
/// // Use derived implementation to print the vikings.
9696
/// for x in &vikings {
@@ -104,7 +104,7 @@ use super::map::{self, HashMap, Keys, RandomState};
104104
/// use std::collections::HashSet;
105105
///
106106
/// fn main() {
107-
/// let viking_names: HashSet<&str> =
107+
/// let viking_names: HashSet<&'static str> =
108108
/// [ "Einar", "Olaf", "Harald" ].iter().cloned().collect();
109109
/// // use the values stored in the set
110110
/// }

0 commit comments

Comments
 (0)