Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

expected struct `VecDeque<Student>` found struct `VecDeque<Student>` (`Student`) – Rust

I’m learning Rust and I’m practicing Traits and generic. I made the following code. I don’t understand why it isn’t working, i mean, i can’t see why the mistake.

struct Student {
    name: String,
    age: u8,
    course: String
}

struct School {
    students: VecDeque<Student>
}

trait Database<T> {
    fn get_database(&self) -> &VecDeque<T>;
}

impl<Student> Database<Student> for School {
    fn get_database(&self) -> &VecDeque<Student> {
        &self.students
    }
}

The compiler points out: 
  --> src\main.rs:25:9
   |
23 | impl<Student> Database<Student> for School {
   |      ------- expected this type parameter
24 |     fn get_database(&self) -> &VecDeque<Student> {
   |                               ------------------ expected `&VecDeque<Student>` because of return type
25 |         &self.students
   |         ^^^^^^^^^^^^^^ expected type parameter `Student`, found `Student`
   |
   = note: expected reference `&VecDeque<Student>` (type parameter `Student`)
              found reference `&VecDeque<Student>` (`Student`)

I dont understand what i’m doing wrong because self.students is of type VecDeque and the reference of this type is being returned from the method.
Thanks!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This line is your problem:

impl<Student> Database<Student> for School {

impl<Student> is essentially the same as impl<T>. You are declaring a generic type parameter called Student, which can be any type at all. The rest of the uses of Student in the impl block refer to this generic type, not the Student struct.

If you read the error message with this understanding, it starts to make sense. Note that it refers to the Student in impl<Student> as a type parameter, which is not the same thing as a proper type. In particular, this line:

expected type parameter `Student`, found `Student`

The compiler is telling you it expected to find a &VecDeque<Student> where Student is the generic type, but it actually found a &VecDeque<Student> where Student is the struct named Student.

To fix the issue, just change impl<Student> to impl:

impl Database<Student> for School {

This declares a non-generic implementation of Database<T> for Student that only covers the case where T is the Student struct.

(Playground)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading