I'm trying to create a type alias of a Peekable slice::Iter, but the compiler keeps complaining that I need a lifetime parameter.
This iterator is used in several places, and always iterates over the same type. To make the code more concise, I would like to use a type alias.
I have already tried (code examples below):
- Adding a lifetime parameter to the struct it is iterating over, and pass this as the argument to the generic, to no avail.
- Adding a lifetime parameter inside the iterator
use std::{iter::Peekable, slice::Iter};
pub struct MyStruct {
pub arg1: i32,
pub arg2: i32,
pub arg3: MyEnum,
}
pub enum MyEnum {
Default,
}
// mutable since I want to call .peek() and .next() on my iterator
type MyIterator<'a> = &'a mut Peekable<Iter<MyStruct>>;
Notice that the above works just fine if I don't use a type alias, i.e. I use it directly in code:
pub fn my_fn(it: &mut Peekable<Iter<MyStruct>>) -> i32 { /* ... */ }
I keep getting the following error:
error[E0106]: missing lifetime specifier
--> src/lib.rs:14:40
|
14 | type MyIterator<'a> = &'a mut Peekable<Iter<MyStruct>>;
| ^^^^^^^^^^^^^^ expected lifetime parameter
I have also tried:
... = &'a Peekable<Iter<&'a MyStruct>>;... = &'a Peekable<Iter<MyStruct+ 'a>>;
Neither work, even when adding a lifetime parameter to the MyStruct. I don't really understand how Rust wants me to define the lifetime parameter. (In general I don't fully understand this concept yet as I just started programming in Rust)