rust-lang / rust-lang/rust

Cannot infer type using `as _` while indexing

Open
#145,178 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-coercions A-inference C-enhancement T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

When trying to index an array with another type that is not usize that can be converted to it using as _ inference fails.

Example code:

fn main() {
    let array = [0, 1, 1, 2, 3, 5];
    let index: u32 = 1;
    array[index as _];
}

Error:

error[E0282]: type annotations needed
 --> src/main.rs:4:20
  |
4 |     array[index as _];
  |                    ^ cannot infer type

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the example in src/main.rs and inspect how the compiler handles as _ during array indexing and type inference. Confirm the intended inference behavior, then add regression coverage and verify that the example no longer produces E0282.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.