What does &argument: Type mean?

I came across an error message by trying to compile something like this:

use std::fmt::Debug;

pub fn function(thing: dyn Debug) {}

There is the obvious message about the argument not being Sized:

error[E0277]: the size for values of type `(dyn Debug + 'static)` cannot be known at compilation time
 --> src/main.rs:3:17
  |
3 | pub fn function(thing: dyn Debug) {}
  |                 ^^^^^ doesn't have a size known at compile-time
  |
  = help: the trait `Sized` is not implemented for `(dyn Debug + 'static)`
help: function arguments must have a statically known size, borrowed types always have a known size
  |
3 | pub fn function(&thing: dyn Debug) {}
  |                 ^

But the help suggests something that I have never seen before. And I've been doing rust for a while now. It seems similar to how you would use the first argument in a function in an impl block but also giving a type? Is it just syntax sugar? What does it mean? :face_with_raised_eyebrow:


If I do do what it suggests I get two more errors:

The first is saying that there is a type mismatch. What type mismatch? Why is it expecting a reference?

The second is still telling me off about it not being Sized but this time the suggestion makes more sense to me. An &dyn Debug has the size of a reference so it is sized.

error[E0308]: mismatched types
 --> src/main.rs:3:17
  |
3 | pub fn function(&thing: dyn Debug) {}
  |                 ^^^^^^-----------
  |                 |       |
  |                 |       expected due to this
  |                 expected trait object `dyn Debug`, found reference
  |                 help: did you mean `thing`: `&(dyn Debug + 'static)`
  |
  = note: expected trait object `(dyn Debug + 'static)`
                found reference `&_`

error[E0277]: the size for values of type `(dyn Debug + 'static)` cannot be known at compilation time
 --> src/main.rs:3:17
  |
3 | pub fn function(&thing: dyn Debug) {}
  |                 ^^^^^^ doesn't have a size known at compile-time
  |
  = help: the trait `Sized` is not implemented for `(dyn Debug + 'static)`
help: function arguments must have a statically known size, borrowed types always have a known size
  |
3 | pub fn function(&thing: &dyn Debug) {}
  |                         ^

So I do the second suggestion I get another error:

error[E0033]: type `&dyn Debug` cannot be dereferenced
 --> src/main.rs:3:17
  |
3 | pub fn function(&thing: &dyn Debug) {}
  |                 ^^^^^^ type `&dyn Debug` cannot be dereferenced

Why would it need to be dereferenced?


I guess I am just really confused about what that &argument: Type means. It doesn't even work when I do something like:

fn function(&thing: usize) {}

It says the same thing about expecting a reference but finding usize.


On another note is this error message misleading (the help seems like it is)? What is the usual way to go when you find a bad error message if you want to help out the community?

You can report the bad error message to Rust here. As for the meaning, the argument of a function is allowed to be a pattern, and you can read about the meaning of & in patterns here.

2 Likes

The argument of a function can be a pattern? Wow I had no idea. I'll see what I can find by some googling. Now that I know it can be a pattern it makes more sense.

The suggestion is good on nightly (beta and stable are the same right now). So I guess it was fixed. Now at least I know how it can be reported.

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.