Rust errors / Types and traits
Rust compiler error · checked with rustc 1.99.0
Rust error E0283: type annotations needed
What it meansThe compiler could not infer a type and asked for a type annotation.
What rustc prints
error[E0283]: type annotations needed --> main.rs:3:5 | 3 | let x = "hello".chars().rev().collect(); | ^ ------- type must be known at this point | = note: multiple `impl`s satisfying `_: FromIterator<char>` found in the `alloc` crate: - impl FromIterator<char> for Box<str>; - impl FromIterator<char> for ByteString; - impl FromIterator<char> for String; note: required by a bound in `collect` --> /rustc/b940084d7eb6a299eb4bfeb8e34901bc051e7ac4/library\core\src\iter\traits\iterator.rs:2073:4 help: consider giving `x` an explicit type | 3 | let x: Vec<_> = "hello".chars().rev().collect(); | ++++++++
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.
The compiler team's explanation
The compiler could not infer a type and asked for a type annotation.
Erroneous code example:
let x = "hello".chars().rev().collect();
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.
A common example is the collect method on Iterator. It has a generic type parameter with a FromIterator bound, which for a char iterator is implemented by Vec and String among others. Consider the following snippet that reverses the characters of a string:
In the first code example, the compiler cannot infer what the type of x should be: Vec<char> and String are both suitable candidates. To specify which type to use, you can use a type annotation on x:
let x: Vec<char> = "hello".chars().rev().collect();
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<_>>();
We can see a self-contained example below:
struct Foo;
impl Into<u32> for Foo {
fn into(self) -> u32 { 1 }
}
let foo = Foo;
let bar: u32 = foo.into() * 1u32;
This error can be solved by adding type annotations that provide the missing information to the compiler. In this case, the solution is to specify the trait's type parameter:
struct Foo;
impl Into<u32> for Foo {
fn into(self) -> u32 { 1 }
}
let foo = Foo;
let bar: u32 = Into::<u32>::into(foo) * 1u32;From rustc --explain E0283 (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?