Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Suggest to use .chars().nth(x) or .as_bytes() when trying to index str #57055

Closed
ariasuni opened this issue Dec 22, 2018 · 1 comment
Closed
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one.

Comments

@ariasuni
Copy link
Contributor

It’s a pain point for beginners that they cannot index Strings or strs. It would make it much less frustrating if they got an helpful error message.

Right now, we get this error message:

error[E0277]: the type `str` cannot be indexed by `{integer}`
 --> main.rs:2:18
  |
2 |     let _value = "test"[0];
  |                  ^^^^^^^^^ `str` cannot be indexed by `{integer}`
  |
  = help: the trait `std::ops::Index<{integer}>` is not implemented for `str`

But it’s not clear what we should do, and the error message doesn’t guide the user toward the solution. It would be better if there was an error message as the following instead:

error[EXXXX]: cannot index `String` or string slice (`str`)
 --> main.rs:2:24
  |
  |     let _value = "test"[0];
  |                        ^^^ cannot index here
  |
  |
  = help: consider using `.chars()` or `.to_bytes()`
@frewsxcv frewsxcv added A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. labels Dec 22, 2018
@estebank
Copy link
Contributor

Thanks for the report.

Closing as duplicate of #56740.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one.
Projects
None yet
Development

No branches or pull requests

3 participants