Trait returning Vec<Self> gives error

Hello rust community

I created a trait that returns a Result of Vec. But I am getting this error "size of values of Self cannot be known at compilation time. Can you help me understand why this error is happening, and how to fix it?

error[E0277]: the size for values of type `Self` cannot be known at compilation time
   --> src/schema/
19  |     ) -> Result<Vec<Self>, sqlx::Error>
    |                 ^^^^^^^^^ doesn't have a size known at compile-time
note: required by a bound in `Vec`
   --> /Users/leen.alhaimy/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/src/rust/library/alloc/src/vec/
400 | pub struct Vec<T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global> {
    |                ^ required by this bound in `Vec`

It's because it's possible to implement traits on unsized types like [T], which wouldn't work with that signature.

You either need to change the trait to trait YourTraitName: Sized { … } or add a where Self: Sized bound to the method to say, respectively, that "you can only implement this trait for Sized types" or "you can only call this method for Sized types".

1 Like