Rust errors / Types and traits
Rust compiler error · checked with rustc 1.99.0
Rust error E0284: type annotations needed
What it meansThis error occurs when the compiler is unable to unambiguously infer the return type of a function or method which is generic on return type, such as the collect method for Iterators.
What rustc prints
error[E0284]: type annotations needed --> main.rs:4:15 | 4 | d = d + n.into(); | - ^^^^ | | | type must be known at this point | = note: cannot satisfy `<u64 as Add<_>>::Output == u64` = note: multiple `impl`s satisfying `u64: Add<_>` found in the `core` crate: - impl Add for u64; - impl Add<&u64> for u64; help: try using a fully qualified path to specify the expected types | 4 - d = d + n.into(); 4 + d = d + <u32 as Into<T>>::into(n); |
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
This error occurs when the compiler is unable to unambiguously infer the return type of a function or method which is generic on return type, such as the collect method for Iterators.
For example:
fn main() {
let n: u32 = 1;
let mut d: u64 = 2;
d = d + n.into();
}
Here we have an addition of d and n.into(). Hence, n.into() can return any type T where u64: Add<T>. On the other hand, the into method can return any type where u32: Into<T>.
The author of this code probably wants into() to return a u64, but the compiler can't be sure that there isn't another type T where both u32: Into<T> and u64: Add<T>.
To resolve this error, use a concrete type for the intermediate expression:
fn main() {
let n: u32 = 1;
let mut d: u64 = 2;
let m: u64 = n.into();
d = d + m;
}From rustc --explain E0284 (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?