Improve documentation and error message for Vec type on erroneous indexing #36645
Labels
A-diagnostics
Area: Messages for errors, warnings, and lints
C-enhancement
Category: An issue proposing an enhancement or a PR with one.
Note Similar issue to #31062
Documentation problem
The error for trying to index
Vec
with the wrong type isn't clear. A couple of examples:The error for this is:
The error:
The error hints that
Vec
implements theIndex
trait, but it doesn't say that it implements it forusize
specifically. Instead, it says that it doesn't implementIndex
for the type you tried to index with.This is very confusing for beginners, finding the answer involves a knowledge of:
Index
Vec
implements:impl<T> Index<usize> for Vec<T>
impl<T> IndexMut<usize> for Vec<T>
impl<T> Index<Range<usize>> for Vec<T>
usize
for indexingWhile it makes the most sense that
usize
is used, it might not be obvious at first (specially for beginners like me). Trying to index with any other numerical primitive (i8
,u8
, etc.) will result in the error shown above.Possible solutions
Vec
implements indexing forusize
only. Maybe something like:rustc --explain E0277
Vec
, the use ofusize
is implied in the doc examples and is explicitly stated in the part that shows what traitsVec
implements - both hard for beginners to find and comprehendThe text was updated successfully, but these errors were encountered: