Skip to content

HashMap using &str keys is not a a realistic example #46966

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

Closed
kornelski opened this issue Dec 23, 2017 · 8 comments
Closed

HashMap using &str keys is not a a realistic example #46966

kornelski opened this issue Dec 23, 2017 · 8 comments
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools C-enhancement Category: An issue proposing an enhancement or a PR with one.

Comments

@kornelski
Copy link
Contributor

The example in the docs uses HashMap of &str:

https://doc.rust-lang.org/std/collections/struct.HashMap.html#examples

but this gives bad example to users who copy the example without fully realizing consequences of using &str instead of String. Here's thread by a new user lead down a confusing path because of this example: https://users.rust-lang.org/t/how-to-pass-slice-of-structs-string-field/14586

I suggest using String instead, even if that means a bit more noise from to_owned().

@kennytm kennytm added C-enhancement Category: An issue proposing an enhancement or a PR with one. A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools labels Dec 23, 2017
@GuillaumeGomez
Copy link
Member

I'm not 100% sure it's a good idea. The point is to show how to use it. It'd give a bad example if we used String instead of &str when it's not needed, don't you think?

@kornelski
Copy link
Contributor Author

kornelski commented Jan 6, 2018

No, I think String is clearly better.

For advanced users it doesn't matter, as they'll see the HashMap API regardless of key type used.

But for novice users nudging towards String is much safer — it's more likely to be the right thing. The worst case is some overhead and need to call .to_string(), which is easier to figure out for novice users than whack-a-mole of lifetime annotations and borrow checker errors.

I've filed this issue because this example has actually mislead a new Rust user.

@GuillaumeGomez
Copy link
Member

The issue is kinda the same: either we show users that they should use String all the time (even when unneeded) or we show an example that we'll let them think that they should always use &str instead of String. Complicated writing doc is...

@JoshLambda
Copy link

JoshLambda commented May 26, 2018

Choosing &str over String is a matter of always using idiomatic code in examples. (It is not a matter of overhead or optimization.)
Using String in this example would not be the idiomatic way to proceed. So, to use String and still have idiomatic code the example would have to be changed completely.

@kornelski
Copy link
Contributor Author

I've left one example with &str, but changed it to &'static str. I think this way users won't get impression the sets work only with String, but also the extra lifetime will make it easier to find that it's a special case.

@JoshLambda
Copy link

JoshLambda commented May 26, 2018

I understand your intent and I commend you for trying to address this issue. But even if the user can be confused I would still think idiomatic code has the priority, simply because idiomatic code is to be expected in every examples.

In the same way, you wouldn't expect a (natural) language teaching book to give out some weird translations just for orthogonal reasons. In facts, the documentation is the main place where to learn idiomatic code, that's why it should be expected.

I understand this is probably not the best place to debate it though. I'm curious to see the outcome of it.

About your commits, wouldn't using &'static str only at one place make things more confusing? Wouldn't the reader ask himself : "Why did they use &'static str and not String"? Just wondering...

@kornelski
Copy link
Contributor Author

idiomatic code is to be expected in every examples

Note that code packed in a single function, operating on string literals, is not representative of real-world Rust code. What is idiomatic for such a special case, is not idiomatic for Rust in general.

In Rust's own codebase: HashMap<String is in 56 files. HashMap<&'lifetime str is 8 files. HashMap<&str is only in these examples. So these examples are idiomatic examples of un-idiomatic Rust.

bors added a commit that referenced this issue 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`.
@matklad
Copy link
Member

matklad commented Aug 19, 2018

Looks like this is fixed now?

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 C-enhancement Category: An issue proposing an enhancement or a PR with one.
Projects
None yet
Development

No branches or pull requests

5 participants