Skip to content

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

Merged
merged 1 commit into from
Jun 19, 2018

Conversation

kornelski
Copy link
Contributor

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.

@rust-highfive
Copy link
Contributor

r? @sfackler

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 26, 2018
@rust-highfive
Copy link
Contributor

The job x86_64-gnu-llvm-3.9 of your PR failed on Travis (raw log). Through arcane magic we have determined that the following fragments from the build log may contain information about the problem.

Click to expand the log.
[00:45:51] ..........................................................i.........................................
[00:45:55] ....................................................................................................
[00:46:01] ....................................................................................................
[00:46:08] .....................................................................................i..............
[00:46:10] ...iiiiiiiii...................................................
[00:46:10] 
[00:46:10] travis_fold:start:test_ui_nll
travis_time:start:test_ui_nll
Check compiletest suite=ui mode=ui compare_mode=nll (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu)
---
[00:46:59] ..........................................................i.........................................
[00:47:04] ....................................................................................................
[00:47:09] ....................................................................................................
[00:47:15] .....................................................................................i..............
[00:47:18] ...iiiiiiiii...................................................
[00:47:18] 
[00:47:18]  finished in 67.848
[00:47:18] travis_fold:end:test_ui_nll

---
[01:15:23] 
[01:15:23]    Doc-tests std
[01:15:25] 
[01:15:25] running 926 tests
[01:15:57] ii......F...........................................................................................
[01:16:26] ...i......i...i......i..............................................................................
[01:16:34] ....................................................................................................
[01:16:51] ................iiii........ii......................................................................
[01:16:59] ....................................................................................................
[01:16:59] ....................................................................................................
[01:17:15] ...............................................................iiii.................................
[01:17:38] ....................................................................................................
[01:17:48] ...........................................................................iiii.....................
[01:17:52] ..........................
[01:17:52] failures:
[01:17:52] 
[01:17:52] ---- collections/hash/map.rs - collections::hash::map::HashMap (line 274) stdout ----
[01:17:52] error[E0277]: the trait bound `std::string::String: std::borrow::Borrow<&str>` is not satisfied
[01:17:52]   --> collections/hash/map.rs:305:24
[01:17:52]    |
[01:17:52] 34 |     match book_reviews.get(book) {
[01:17:52]    |                        ^^^ the trait `std::borrow::Borrow<&str>` is not implemented for `std::string::String`
[01:17:52]    |
[01:17:52]    = help: the following implementations were found:
[01:17:52]              <std::string::String as std::borrow::Borrow<str>>
[01:17:52] thread 'collections/hash/map.rs - collections::hash::map::HashMap (line 274)' panicked at 'couldn't compile the test', librustdoc/test.rs:325:17
[01:17:52] note: Run with `RUST_BACKTRACE=1` for a backtrace.
[01:17:52] 
[01:17:52] 
---
[01:17:52] 
[01:17:52] error: test failed, to rerun pass '--doc'
[01:17:52] 
[01:17:52] 
[01:17:52] command did not execute successfully: "/checkout/obj/build/x86_64-unknown-linux-gnu/stage0/bin/cargo" "test" "--target" "x86_64-unknown-linux-gnu" "-j" "4" "--release" "--locked" "--color" "always" "--features" "panic-unwind jemalloc backtrace" "--manifest-path" "/checkout/src/libstd/Cargo.toml" "-p" "std" "--" "--quiet"
[01:17:52] 
[01:17:52] 
[01:17:52] failed to run: /checkout/obj/build/bootstrap/debug/bootstrap test
[01:17:52] Build completed unsuccessfully in 0:34:22
[01:17:52] Build completed unsuccessfully in 0:34:22
[01:17:52] Makefile:58: recipe for target 'check' failed
[01:17:52] make: *** [check] Error 1

The command "stamp sh -x -c "$RUN_SCRIPT"" exited with 2.
travis_time:start:015afa76
$ date && (curl -fs --head https://google.com | grep ^Date: | sed 's/Date: //g' || true)

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 @TimNN. (Feature Requests)

/// 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"),
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Member

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.

@sfackler
Copy link
Member

r? @GuillaumeGomez

@GuillaumeGomez
Copy link
Member

GuillaumeGomez commented May 26, 2018

I'm still against it so I'll set another reviewer.

r? @steveklabnik

@shepmaster
Copy link
Member

Ping from triage @steveklabnik !

Copy link
Member

@steveklabnik steveklabnik left a 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!

/// 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"),
Copy link
Member

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.

Copy link
Member

@steveklabnik steveklabnik left a 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 :(

@kornelski
Copy link
Contributor Author

OK, formatted

@pietroalbini pietroalbini added the A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools label Jun 18, 2018
@pietroalbini
Copy link
Member

Ping from triage! This PR needs a review, can @steveklabnik or someone else from @rust-lang/docs review this?

@GuillaumeGomez
Copy link
Member

Basically, only a squash is needed and then it seems to be ready.

@kornelski
Copy link
Contributor Author

Squashed

@steveklabnik
Copy link
Member

Thanks!

@bors: r+ rollup

@bors
Copy link
Collaborator

bors commented Jun 18, 2018

📌 Commit 01e82f1 has been approved by steveklabnik

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 18, 2018
@bors
Copy link
Collaborator

bors commented Jun 19, 2018

⌛ Testing commit 01e82f1 with merge a646c91...

bors added a commit that referenced this pull request Jun 19, 2018
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`.
@bors
Copy link
Collaborator

bors commented Jun 19, 2018

☀️ Test successful - status-appveyor, status-travis
Approved by: steveklabnik
Pushing a646c91 to master...

@bors bors merged commit 01e82f1 into rust-lang:master Jun 19, 2018
@kornelski kornelski deleted the examplestr branch June 19, 2018 15:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants