error[E0308]: mismatched type


#1

Hi I’m pretty new to rust, and just started to play with it and I get this error

error[E0308]: mismatched types
  --> src\widgets\view.rs:70:29
   |
70 |     pub fn build(&mut self, &mut result: Vec<Renderable>) {
   |                             ^^^^^^^^^^^ expected struct `std::vec::Vec`, found &mut _
   |
   = note: expected type `std::vec::Vec<core::renderable::Renderable>`
              found type `&mut _`
   = help: did you mean `mut result: &std::vec::Vec<core::renderable::Renderable>`?

The code itself looks like (the error points to this method)

pub fn build(&mut self, &mut result: Vec<Renderable>) {
    // some stuff...
    result.push(self.renderable);

    for mut row in self.children.iter() {
        row.build(result);
    }
}

And its called like this

let mut result = Vec::new();
self.view.build(result);

Thanks for the help. Regards


#2

Try:

pub fn build(&mut self, result: &mut Vec<Renderable>) {

The code in the “did you mean” part seems sub-optimal.


#3

It worked so well, thanks for the help and quick reply!


#4

A MCVE for this:

fn foo(&mut a: u32) {
    a = 3;
}

fn main() {
    let mut a = 0;
    foo(&mut a);
}

Gives us:

error[E0308]: mismatched types
 --> src/main.rs:1:8
  |
1 | fn foo(&mut a: u32) {
  |        ^^^^^^ expected u32, found &mut _
  |
  = note: expected type `u32`
             found type `&mut _`
  = help: did you mean `mut a: &u32`?

error[E0308]: mismatched types
 --> src/main.rs:7:9
  |
7 |     foo(&mut a);
  |         ^^^^^^
  |         |
  |         expected u32, found mutable reference
  |         help: consider removing the borrow: `a`
  |
  = note: expected type `u32`
             found type `&mut {integer}`

It is a little bit hard for the compiler to tell, what the user actually wants. Does he want to mutate the passed variable (a: &mut u32) or does he want to mutate the copied variable (mut a: u32). Does &mut a: u32 even makes sense at all?

What can be improved on the compiler side? Can clippy do something?