-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Use String, not &str in some collection examples #51081
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
r? @sfackler (rust_highfive has picked a reviewer for you, use r? to override) |
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
src/libstd/collections/hash/map.rs
Outdated
/// book_reviews.insert("Pride and Prejudice", "Very enjoyable."); | ||
/// book_reviews.insert("The Adventures of Sherlock Holmes", "Eye lyked it alot."); | ||
/// // Review some books. | ||
/// book_reviews.insert(String::from("Adventures of Huckleberry Finn"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.to_owned()
is shorter and IMO more expressive.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've used .to_owned()
24eb6069b461fcc337507d0ec0c76db1b1d2c3fb. I'm taking cue from WHATWG here — the HTML5 spec deliberately uses different styles across examples, which gives a broader view of the language.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The standard style, which the docs follow, is to use .to_string()
everywhere.
I'm still against it so I'll set another reviewer. |
Ping from triage @steveklabnik ! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
r=me after every String
creation is changed to .to_string
. Thanks a ton!
src/libstd/collections/hash/map.rs
Outdated
/// book_reviews.insert("Pride and Prejudice", "Very enjoyable."); | ||
/// book_reviews.insert("The Adventures of Sherlock Holmes", "Eye lyked it alot."); | ||
/// // Review some books. | ||
/// book_reviews.insert(String::from("Adventures of Huckleberry Finn"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The standard style, which the docs follow, is to use .to_string()
everywhere.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One last thing I forgot, sorry. The previous formatting wasn't correct, and so this formatting isn't either. rustfmt does
book_reviews.insert(
"Adventures of Huckleberry Finn".to_string(),
"My favorite book.".to_string(),
);
could you fix that as well? r=me after. sorry :(
OK, formatted |
Ping from triage! This PR needs a review, can @steveklabnik or someone else from @rust-lang/docs review this? |
Basically, only a squash is needed and then it seems to be ready. |
Squashed |
Thanks! @bors: r+ rollup |
📌 Commit 01e82f1 has been approved by |
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`.
☀️ Test successful - status-appveyor, status-travis |
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 ofBorrow
.