5

I received this error:

error: mismatched types [E0308] process(s); ^ help: run `rustc --explain E0308` to see a detailed explanation note: expected type `&(u32, u32, image::Luma<u8>)` note: found type `&mut (u32, u32, &mut _)` 

but I do not understand what the _ means.

2
  • 1
    @Shepmaster Sorry. I'll trim down the question. And create new one for "bonus content" with self-response since I already got it. Commented Jun 20, 2016 at 15:32
  • You really do not need the leading &mut here, I assure you. You are complicating the signature way too much :x Just dereference at the call site, your tuple is Copy anyway. Commented Jun 20, 2016 at 15:33

2 Answers 2

6

In Rust, the _ identifier is used when the name does not matter. This occurs in a few situations:

  • let _ = ...; is used to ignore the result of an expression (only necessary when said result is marked with the #[must_use] attribute)
  • In types, _ is used to elide types (either because you prefer to let the compiler infer it or because the compiler judges it does not matter)

Thus, in your case, the compiler has elided the type because it does not matter, so that you can focus on the specific reason of the error. It may be confusing, at first, but once used to it it's actually helpful, especially when types are hairy (very large).


The error is simple: you are mistaking references and values.

Your original signature expects: &(u32, u32, Type) but you pass &mut (u32, u32, &mut Type).

The difference in mutability does not matter (in this direction), however a &mut Type and a Type have very different memory structure and are not interchangeable.

Sign up to request clarification or add additional context in comments.

11 Comments

Can you try to answer bonus question? How to avoid *tup.2 and put it all in function signature?
@KamilSzot: process(&mut (x, y, pixel): &mut (u32,u32,&mut image::Luma<u8>)) I would expect. It's a pattern matching game.
Tried that but "cannot move out of borrowed content".
@KamilSzot: Ah... why do you have that &mut in front by the way? It's unused here. Shouldn't the signature be process((x, y, pixel): (u32, u32, &mut image::Luma<u8>)) ?
I need it. Without it, rust complains about mismatch between (_, _, _) and &mut (u32, u32, &mut image::Luma<u8>)
|
4

The _ is just a placeholder that could mean anything, and helps to clarify the error message.

In this case, it's saying it was expecting an image::Luma<u8>, but it actually found a mutable reference (&mut) to something. It doesn't matter what that something is; it &mut <something> can never be matched with image::Luma<i>.

If instead it said ...found type &mut (u32, u32, &mut T: SomeTrait) or similar, I think it would be harder to zoom in to the immediate problem.

You can't pass a reference to a function that expects an copied (or moved) full object instead.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.