Closed
Description
It’s a pain point for beginners that they cannot index String
s or str
s. 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()`