Open
Description
When comparing a newly created empty Vec::<&i32>
or Vec::<Vec<i32>>
with an empty array literal []
, the compiler fails to infer the type and gives error E0282
, even though the types seem unambiguous from the context.
I tried this code:
fn test1() {
assert_eq!(Vec::<i32>::new(), []);
}
fn test2() {
assert_eq!(Vec::<&i32>::new(), []);
}
fn test3() {
assert_eq!(Vec::<Vec<i32>>::new(), []);
}
Only test1 compiles. test2 and test3 fail with: error[E0282]: type annotations needed
I expected the compiler to infer the type of [] from the left-hand side (Vec::<&i32> or Vec::<Vec>), like it does in test1.
Instead, this happened: Type inference fails unless I add an explicit type to the right-hand side, or avoid [] altogether.
Meta
rustc --version --verbose
:
rustc 1.86.0 (05f9846f8 2025-03-31)
binary: rustc
commit-hash: 05f9846f893b09a1be1fc8560e33fc3c815cfecb
commit-date: 2025-03-31
host: aarch64-apple-darwin
release: 1.86.0
LLVM version: 19.1.7