Skip to content

error[E0282]: type annotations needed but should be inferred automatically #139168

Open
@Zercerium

Description

@Zercerium

Hi, I tried to search for a similar issue but couldn't find one. I hope I haven't missed any. Though this issue seems quite obvious. Thanks a lot for your time and work!

I tried this code: (minimal reproducible)

#[derive(Debug,Default)]
struct Something {
   value: i32,
}

impl Something {
    pub fn new() -> Self {
        let mut something = des();
        something.value = 100; // comment out and everything works just fine
        something
    }
}

fn des<T: Default>() -> T {
    T::default()
}

fn main() {
    let deserialized = Something::new();
    println!("deserialized = {:?}", deserialized);
}

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=59137d08e7fcf67e6379022640676ebe

I expected to see this happen: infer the type automatically and compile successfully, like rust-analyser

Instead, this happened:

error[E0282]: type annotations needed
  --> src/main.rs:10:13
   |
10 |         let mut something = des();
   |             ^^^^^^^^^^^^^
11 |         something.value = 100; // comment out and everything works just fine 
   |         --------- type must be known at this point
   |
help: consider giving `something` an explicit type
   |
10 |         let mut something: /* Type */ = des();
   |                          ++++++++++++

For more information about this error, try `rustc --explain E0282`.
error: could not compile `playground` (bin "playground") due to 1 previous error

Meta

rustc --version --verbose:

rustc 1.85.1 (4eb161250 2025-03-15)
binary: rustc
commit-hash: 4eb161250e340c8f48f66e2b929ef4a5bed7c181
commit-date: 2025-03-15
host: aarch64-apple-darwin
release: 1.85.1
LLVM version: 19.1.7

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-inferenceArea: Type inferenceC-enhancementCategory: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-typesRelevant to the types team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions