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:
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?