Skip to content

Type inference fails to determine closure argument type #12679

Open
@edwardw

Description

@edwardw

The following fails to compile:

pub fn main() {
  let mut xs = HashMap::<(u32, u32), u32>::new();
  let new_el = |&_| 30;  // error: the type of this value must be known in this context

  xs.insert((1,1), 10);
  xs.insert((2,2), 20);
  xs.find_or_insert_with((3,3), new_el);

  println!("{}", xs);
}

The argument type must be explicitly spelled out:

let new_el = |_: &(u32, u32)| 30;

Should the type inference work here?

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-closuresArea: Closures (`|…| { … }`)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