From c120199116439827a89a0ac0f24f91f99dbed60f Mon Sep 17 00:00:00 2001 From: folex <0xdxdy@gmail.com> Date: Sat, 5 Jan 2019 05:56:23 +0300 Subject: [PATCH] Show suggestion to use .char().nth() and link to The Book on unimplemented Index trait --- src/libcore/ops/index.rs | 30 ++++++++++++++++++++++++++++++ src/test/ui/str/str-idx.stderr | 2 ++ src/test/ui/str/str-mut-idx.stderr | 2 ++ 3 files changed, 34 insertions(+) diff --git a/src/libcore/ops/index.rs b/src/libcore/ops/index.rs index 4f55c68ecd4ae..6cfa36741d0ca 100644 --- a/src/libcore/ops/index.rs +++ b/src/libcore/ops/index.rs @@ -51,6 +51,21 @@ /// ``` #[lang = "index"] #[rustc_on_unimplemented( + on( + _Self="&str", + note="you can use `.chars().nth()` or `.bytes().nth()` +see chapter in The Book " + ), + on( + _Self="str", + note="you can use `.chars().nth()` or `.bytes().nth()` +see chapter in The Book " + ), + on( + _Self="std::string::String", + note="you can use `.chars().nth()` or `.bytes().nth()` +see chapter in The Book " + ), message="the type `{Self}` cannot be indexed by `{Idx}`", label="`{Self}` cannot be indexed by `{Idx}`", )] @@ -141,6 +156,21 @@ pub trait Index { /// ``` #[lang = "index_mut"] #[rustc_on_unimplemented( + on( + _Self="&str", + note="you can use `.chars().nth()` or `.bytes().nth()` +see chapter in The Book " + ), + on( + _Self="str", + note="you can use `.chars().nth()` or `.bytes().nth()` +see chapter in The Book " + ), + on( + _Self="std::string::String", + note="you can use `.chars().nth()` or `.bytes().nth()` +see chapter in The Book " + ), message="the type `{Self}` cannot be mutably indexed by `{Idx}`", label="`{Self}` cannot be mutably indexed by `{Idx}`", )] diff --git a/src/test/ui/str/str-idx.stderr b/src/test/ui/str/str-idx.stderr index 108096df9c41c..71b1747492329 100644 --- a/src/test/ui/str/str-idx.stderr +++ b/src/test/ui/str/str-idx.stderr @@ -5,6 +5,8 @@ LL | let c: u8 = s[4]; //~ ERROR the type `str` cannot be indexed by `{integ | ^^^^ `str` cannot be indexed by `{integer}` | = help: the trait `std::ops::Index<{integer}>` is not implemented for `str` + = note: you can use `.chars().nth()` or `.bytes().nth()` + see chapter in The Book error: aborting due to previous error diff --git a/src/test/ui/str/str-mut-idx.stderr b/src/test/ui/str/str-mut-idx.stderr index a8ab38e5ab6a5..f1e969696b458 100644 --- a/src/test/ui/str/str-mut-idx.stderr +++ b/src/test/ui/str/str-mut-idx.stderr @@ -29,6 +29,8 @@ LL | s[1usize] = bot(); | ^^^^^^^^^ `str` cannot be mutably indexed by `usize` | = help: the trait `std::ops::IndexMut` is not implemented for `str` + = note: you can use `.chars().nth()` or `.bytes().nth()` + see chapter in The Book error: aborting due to 3 previous errors