Unclear error with type inference failure

3206876
Opened by jethrogb at 2023-08-14 13:49:31
struct S {
    v: Vec<(u32, Vec<u32>)>,
}

impl S {
    pub fn remove(&mut self, i: u32) -> Option<std::vec::Drain<u32>> {
        self.v.get_mut(i as _).map(|&mut (_, ref mut v2)| {
            v2.drain(..)
        })
    }
}

Errors with:

error: the type of this value must be known in this context
 --> src/main.rs:8:16
  |
8 |             v2.drain(..)
  |                ^^^^^

I'm not sure which value the error is pointing at here. The fix here is to explicitly specify the type we want to cast i into.

  1. Current output:

    error[E0282]: type annotations needed for `&mut (_, _)`
     --> file8.rs:7:37
      |
    7 |         self.v.get_mut(i as _).map(|&mut (_, ref mut v2)| {
      |                                     ^^^^^^^^^^^^^^^^^^^^ consider giving this closure parameter the explicit type `&mut (_, _)`, with the type parameters specified
      |
      = note: type must be known at this point
    

    Esteban Kuber at 2019-10-15 18:07:56

  2. Output has regressed to the original output on nightly.

    Esteban Kuber at 2022-06-08 22:00:16

  3. It's not quite the same as the original:

    error[E0282]: type annotations needed
     --> src/lib.rs:8:16
      |
    8 |             v2.drain(..)
      |                ^^^^^ cannot infer type
      |
      = note: type must be known at this point
    

    jethrogb at 2022-06-09 16:15:22

  4. Current output:

    error[E0282]: type annotations needed for `&mut (_, _)`
     --> f71.rs:7:37
      |
    7 |         self.v.get_mut(i as _).map(|&mut (_, ref mut v2)| {
      |                                     ^^^^^^^^^^^^^^^^^^^^
    8 |             v2.drain(..)
      |                ----- type must be known at this point
      |
    help: consider giving this closure parameter an explicit type, where the placeholders `_` are specified
      |
    7 |         self.v.get_mut(i as _).map(|&mut (_, ref mut v2): &mut (_, _)| {
      |                                                         +++++++++++++
    

    Esteban Kuber at 2023-08-14 13:49:31