Skip to content

Cannot infer type using as _ while indexing #145178

@panpanpro888

Description

@panpanpro888

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-coercionsArea: implicit and explicit `expr as Type` coercionsA-inferenceArea: Type inferenceC-enhancementCategory: An issue proposing an enhancement or a PR with one.T-typesRelevant to the types team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions