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? ![]()
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?