Rust errors / Types and traits
Rust compiler error · checked with rustc 1.99.0
Rust error E0282: type annotations needed
What it meansRust cannot work out a type from the code alone and needs you to name it. Common with collect(), parse(), Default::default() and empty collections.
What rustc prints
error[E0282]: type annotations needed for `Vec<_>` --> main.rs:3:5 | 3 | let x = Vec::new(); | ^ ---------- type must be known at this point | help: consider giving `x` an explicit type, where the type for type parameter `T` is specified | 3 | let x: Vec<T> = Vec::new(); | ++++++++
Real output of rustc 1.99.0 compiling the official example below (2021 edition). Your names and line numbers will differ; the first line is the part to match.
Why it happens
let v = s.split(',').collect();without saying what to collect into."42".parse()without a target type.let v = Vec::new();that is never pushed to.
How to fix it
1. Annotate the variable
let parts: Vec<&str> = s.split(',').collect();
let n: u32 = "42".parse().unwrap();2. Use the turbofish
Name the type on the call.
let parts = s.split(',').collect::<Vec<_>>();
let n = "42".parse::<u32>().unwrap();Other wordings of this error
type annotations needed, cannot infer type, consider giving this a type
The compiler team's explanation
The compiler could not infer a type and asked for a type annotation.
Erroneous code example:
let x = Vec::new();
This error indicates that type inference did not result in one unique possible type, and extra information is required. In most cases this can be provided by adding a type annotation. Sometimes you need to specify a generic type parameter manually.
In the example above, type Vec has a type parameter T. When calling Vec::new, barring any other later usage of the variable x that allows the compiler to infer what type T is, the compiler needs to be told what it is.
The type can be specified on the variable:
let x: Vec<i32> = Vec::new();
The type can also be specified in the path of the expression:
let x = Vec::<i32>::new();
In cases with more complex types, it is not necessary to annotate the full type. Once the ambiguity is resolved, the compiler can infer the rest:
let x: Vec<_> = "hello".chars().rev().collect();
Another way to provide the compiler with enough information, is to specify the generic type parameter:
let x = "hello".chars().rev().collect::<Vec<char>>();
Again, you need not specify the full type if the compiler can infer it:
let x = "hello".chars().rev().collect::<Vec<_>>();
Apart from a method or function with a generic type parameter, this error can occur when a type parameter of a struct or trait cannot be inferred. In that case it is not always possible to use a type annotation, because all candidates have the same return type. For instance:
struct Foo<T> {
num: T,
}
impl<T> Foo<T> {
fn bar() -> i32 {
0
}
fn baz() {
let number = Foo::bar();
}
}
This will fail because the compiler does not know which instance of Foo to call bar on. Change Foo::bar() to Foo::<T>::bar() to resolve the error.
From rustc --explain E0282 (1.99.0), part of the Rust project, MIT or Apache-2.0. Official page.
Related errors
Up next
Often hit nextE0308: mismatched typesA related error from the same family.Read it next →Did this answer your question?