Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Improve documentation and error message for Vec type on erroneous indexing #36645

Closed
andradei opened this issue Sep 22, 2016 · 3 comments
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

@andradei
Copy link

andradei commented Sep 22, 2016

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:

  • Example 1
vec![0][0i32]

The error for this is:

error: the trait bound `std::vec::Vec<_>: std::ops::Index<i32>` is not satisfied [--explain E0277]
  • Example 2
fn main() {
    let n = 5u32; // This value could come from a struct member or function call.
    let vec: Vec<i32> = vec![1, 2, 3, 4, 5];

    for i in 0..n {
        println!("{}", vec[i])
    }
}

The error:

error: the trait bound `std::vec::Vec<i32>: std::ops::Index<u32>` is not satisfied [--explain E0277]

The error hints that Vec implements the Index trait, but it doesn't say that it implements it for usize specifically. Instead, it says that it doesn't implement Index for the type you tried to index with.

This is very confusing for beginners, finding the answer involves a knowledge of:

  • Traits and how they work
  • Rust's type behavior (due to its powerful type system) many times comes from implementing traits
  • Indexing on a type is possible via a trait called 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>
    • And a bunch or others, all using usize for indexing

While 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

error: the trait bound `std::vec::Vec<i32>: std::ops::Index<u32>` is not satisfied.
std::vec::Vec<T> implements std:ops:Index<usize>, implements std:ops:Index<Range<usize>>, etc...
[--explain E0277]
  • Add that info on rustc --explain E0277
  • Add this information in the documentation for Vec, the use of usize is implied in the doc examples and is explicitly stated in the part that shows what traits Vec implements - both hard for beginners to find and comprehend
@andradei andradei changed the title Better error message for Vec type on erroneous indexing Improve documentation and error message for Vec type on erroneous indexing Sep 22, 2016
@GuillaumeGomez
Copy link
Member

It used to work: #33401

@andradei
Copy link
Author

I thought it worked only for slices. Anyways, it would be nice to see the same message for Vec.

@Mark-Simulacrum Mark-Simulacrum added the A-diagnostics Area: Messages for errors, warnings, and lints label May 13, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jul 26, 2017
@estebank
Copy link
Contributor

estebank commented Mar 6, 2019

Current output

error[E0277]: the trait bound `u32: std::slice::SliceIndex<[i32]>` is not satisfied
 --> src/main.rs:7:24
  |
7 |         println!("{}", vec[i])
  |                        ^^^^^^ slice indices are of type `usize` or ranges of `usize`
  |
  = help: the trait `std::slice::SliceIndex<[i32]>` is not implemented for `u32`
  = note: required because of the requirements on the impl of `std::ops::Index<u32>` for `std::vec::Vec<i32>`

@estebank estebank closed this as completed Mar 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in 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

4 participants