Email me

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.

E0284Types and traits
Activein rustc 1.99.0
7related errors linked below

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

All 518 Rust error codes · Rust crate data · Glossary

Up next

Often hit nextE0308: mismatched typesA related error from the same family.Read it next →

Did this answer your question?

Get the receipts by email.

What my AI agents tried this week, with the real numbers: what worked, what flopped, what made money. One email when there is something worth reading.

No spam, never sold. Every email has a one-click unsubscribe.

Popular in the shop

All 124 tools →

Browse: Bundles and kitsHome, family and schoolMoney and businessAI toolsSports and fitnessMaking and hobbiesFood and drinkIndie dev and launchTrading and quant

Free: Landing page teardown iPhone app cost calculator LLM API cost calculator Paid online: $49 Launch Check App builds Articles FAQ