Skip to content

Commit c120199

Browse files
committed
Show suggestion to use .char().nth() and link to The Book on unimplemented Index trait
1 parent bf6bb14 commit c120199

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

src/libcore/ops/index.rs

+30
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,21 @@
5151
/// ```
5252
#[lang = "index"]
5353
#[rustc_on_unimplemented(
54+
on(
55+
_Self="&str",
56+
note="you can use `.chars().nth()` or `.bytes().nth()`
57+
see chapter in The Book <https://doc.rust-lang.org/book/ch08-02-strings.html#indexing-into-strings>"
58+
),
59+
on(
60+
_Self="str",
61+
note="you can use `.chars().nth()` or `.bytes().nth()`
62+
see chapter in The Book <https://doc.rust-lang.org/book/ch08-02-strings.html#indexing-into-strings>"
63+
),
64+
on(
65+
_Self="std::string::String",
66+
note="you can use `.chars().nth()` or `.bytes().nth()`
67+
see chapter in The Book <https://doc.rust-lang.org/book/ch08-02-strings.html#indexing-into-strings>"
68+
),
5469
message="the type `{Self}` cannot be indexed by `{Idx}`",
5570
label="`{Self}` cannot be indexed by `{Idx}`",
5671
)]
@@ -141,6 +156,21 @@ pub trait Index<Idx: ?Sized> {
141156
/// ```
142157
#[lang = "index_mut"]
143158
#[rustc_on_unimplemented(
159+
on(
160+
_Self="&str",
161+
note="you can use `.chars().nth()` or `.bytes().nth()`
162+
see chapter in The Book <https://doc.rust-lang.org/book/ch08-02-strings.html#indexing-into-strings>"
163+
),
164+
on(
165+
_Self="str",
166+
note="you can use `.chars().nth()` or `.bytes().nth()`
167+
see chapter in The Book <https://doc.rust-lang.org/book/ch08-02-strings.html#indexing-into-strings>"
168+
),
169+
on(
170+
_Self="std::string::String",
171+
note="you can use `.chars().nth()` or `.bytes().nth()`
172+
see chapter in The Book <https://doc.rust-lang.org/book/ch08-02-strings.html#indexing-into-strings>"
173+
),
144174
message="the type `{Self}` cannot be mutably indexed by `{Idx}`",
145175
label="`{Self}` cannot be mutably indexed by `{Idx}`",
146176
)]

src/test/ui/str/str-idx.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ LL | let c: u8 = s[4]; //~ ERROR the type `str` cannot be indexed by `{integ
55
| ^^^^ `str` cannot be indexed by `{integer}`
66
|
77
= help: the trait `std::ops::Index<{integer}>` is not implemented for `str`
8+
= note: you can use `.chars().nth()` or `.bytes().nth()`
9+
see chapter in The Book <https://doc.rust-lang.org/book/ch08-02-strings.html#indexing-into-strings>
810

911
error: aborting due to previous error
1012

src/test/ui/str/str-mut-idx.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ LL | s[1usize] = bot();
2929
| ^^^^^^^^^ `str` cannot be mutably indexed by `usize`
3030
|
3131
= help: the trait `std::ops::IndexMut<usize>` is not implemented for `str`
32+
= note: you can use `.chars().nth()` or `.bytes().nth()`
33+
see chapter in The Book <https://doc.rust-lang.org/book/ch08-02-strings.html#indexing-into-strings>
3234

3335
error: aborting due to 3 previous errors
3436

0 commit comments

Comments
 (0)