Skip to content

Expected u32 found usize error on array/slice gives unhelpful help text #47168

Closed
@KallDrexx

Description

@KallDrexx
fn main() {
    let array = [1, 2, 3];
    test(array.len());
}

fn test(length: u32) {
    println!("{}", length);
}

Gives the following compile time error on rustc 1.22.1 (stable):

Compiling playground v0.0.1 (file:///playground)
error[E0308]: mismatched types
 --> src/main.rs:3:10
  |
3 |     test(array.len());
  |          ^^^^^^^^^^^ expected u32, found usize
  |
  = help: here are some functions which might fulfill your needs:
          - .count_ones()
          - .count_zeros()
          - .leading_zeros()
          - .trailing_zeros()

error: aborting due to previous error

error: Could not compile `playground`.

I'm assuming there's a lot of nuance in the engine that gives suggestion so I don't know how I would personally codify the rules (and even if casting as a u32 is always appropriate since I guess it could overflow) but I do know that the suggestions definitely do not point me in the right direction.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.WG-diagnosticsWorking group: Diagnostics

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions