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

How to make a trait into a generic on Vec<T>?

I have a working trait:

trait PopOrErr {
    fn pop_or_err(&mut self) -> Result<i8, String>;
}

impl PopOrErr for Vec<i8> {
    fn pop_or_err(&mut self) -> Result<i8, String> {
        self.pop().ok_or_else(|| "stack empty".to_owned())
    }
}

I’ve tried:

trait PopOrErr<T> {
    fn pop_or_err(&mut self) -> Result<T, String>;
}

impl PopOrErr<T> for Vec<T> {
    fn pop_or_err(&mut self) -> Result<T, String> {
        self.pop().ok_or_else(|| "stack empty".to_owned())
    }
}

but it gives me:

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

error[E0412]: cannot find type `T` in this scope
  --> src/main.rs:29:15
   |
29 | impl PopOrErr<T> for Vec<T> {
   |     -         ^ not found in this scope
   |     |
   |     help: you might be missing a type parameter: `<T>`

error[E0412]: cannot find type `T` in this scope
  --> src/main.rs:29:26
   |
29 | impl PopOrErr<T> for Vec<T> {
   |     -                    ^ not found in this scope
   |     |
   |     help: you might be missing a type parameter: `<T>`

error[E0412]: cannot find type `T` in this scope
  --> src/main.rs:30:40
   |
29 | impl PopOrErr<T> for Vec<T> {
   |     - help: you might be missing a type parameter: `<T>`
30 |     fn pop_or_err(&mut self) -> Result<T, String> {
   |                                        ^ not found in this scope

How can I make PopOrErr generic on the type contained within the vector?

>Solution :

"You might be missing a type parameter: <T>" the compiler is trying to help. Put <T> where it says and you are good to go.

trait PopOrErr<T> {
    fn pop_or_err(&mut self) -> Result<T, String>;
}

// Add it here: impl<T>
impl<T> PopOrErr<T> for Vec<T> {
    fn pop_or_err(&mut self) -> Result<T, String> {
        self.pop().ok_or("stack empty".to_string())
    }
}

You have an unrelated error with ok_or_else which I replaced with ok_or above.

If you’re curious as to why you need to put the generic with impl as well as fn check out this question

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