Open
Description
I tried this code:
fn main() {
let v: Vec<Vec<u64>> = vec![vec![7]];
let f = |x, y| v[x][y];
}
I expected to see this happen: code compiles successfully
Instead, this happened:
Compiling playground v0.0.1 (/playground)
error[[E0282]](https://doc.rust-lang.org/stable/error_codes/E0282.html): type annotations needed
--> src/main.rs:3:20
|
3 | let f = |x, y| v[x][y];
| ^^^^ cannot infer type
The strange thing is that type inference for x
and y
work fine, it's the intermediate temporary's type that can't be inferred.
This snippet works:
fn use_closure(_f: impl Fn(usize, usize) -> u64) {}
fn main() {
let v: Vec<Vec<u64>> = vec![vec![7]];
let f = |x, y| -> u64 {
let tmp: &Vec<u64> = &v[x];
tmp[y]
};
use_closure(f);
}
Meta
Version: happens with both 1.86.0 stable and 1.88.0-nightly (2025-04-02 d5b4c2e)