The problem here, as mentioned by the compiler error, is that the lifetime 'a
has no reason to be there if it is implemented like so:
impl<'a, T> Vector for VectorImplementation1<T> {
/**/
}
error[E0207]: the lifetime parameter `'a` is not constrained by the impl
trait, self type, or predicates
--> src/main.rs:13:6
|
13 | impl<'a, T> Vector for VectorImplementation1<T> {
| ^^ unconstrained lifetime parameter
Because the compiler only looks at the definition, and not the body in this case. A different approach, which probably wasn't mentioned earlier for simplicity, is the following:
pub trait Vector<'a> {
type Item: 'a;
type Iter: Iterator<Item = Self::Item> + 'a;
// several functions
fn iter(&'a self) -> Self::Iter;
}
pub struct VectorImplementation1<T> {
numbers: Vec<T>,
}
impl<'a, T: 'a> Vector<'a> for VectorImplementation1<T> {
type Item = &'a T;
type Iter = std::slice::Iter<'a, T>;
fn iter(&'a self) -> Self::Iter {
self.numbers.iter()
}
}
impl<'a, T: 'a> Vector<'a> for &'a VectorImplementation1<T> {
type Item = &'a T;
type Iter = std::slice::Iter<'a, T>;
fn iter(&'a self) -> Self::Iter {
self.numbers.iter()
}
}
In this case, we move the lifetime to the trait, so that the trait can "use" the lifetime, and therefore validate our use of the trait implementation. But as I mentioned earlier, this has the added complexity of needing to understand the lifetimes attached to this trait, reducing readability.