Can't infer the type of index

39d25a8
Opened by yongqli at 2024-09-03 05:45:55

This doesn't type-check, failing with

fn main() {
    let m = [[0.; 2]; 2];
    println!("{}", (|i, j| m[i][j])(0, 1));
}

<anon>:19:28: 19:35 error: the type of this value must be known in this context
<anon>:19     println!("{}", (|i, j| m[i][j])(0, 1));

Annotating as usize fixes it, but I would expect the compiler to be able to infer the type. Is there a reason why it can't?

  1. This doesn't type-check

    fn main() {
        let m = [[0.; 2]; 2];
        println!("{}", (|i, j| m[i][j])(0, 1));
    }
    

    failing with

    error[E0282]: type annotations needed
     --> inf.rs:3:28
      |
    3 |     println!("{}", (|i, j| m[i][j])(0, 1));
      |                            ^^^^ cannot infer type
    

    Annotating as usize fixes it, but I would expect the compiler to be able to infer the type. Is there a reason why it can't?

    León Orell Valerian Liehr at 2024-09-03 05:44:53

  2. See also issue #12679

    bluss at 2015-07-02 08:55:46

  3. Triage: No change.

    Steve Klabnik at 2016-11-29 23:03:45

  4. Triage: no changes

    Steve Klabnik at 2018-10-31 16:00:29

  5. triage: no change

    Maayan Hanin at 2022-03-23 06:43:58