2
votes

To answer one of my previous questions (How to implement a generic trait with a generic type implementing Iterator?), this chunk of code was given to me:

pub trait Vector {
    type Item;
    type Iter: Iterator<Item = Self::Item>;

    // several functions
    fn iter(&self) -> Self::Iter;
}

pub struct VectorImplementation1<T> {
    numbers: Vec<T>,
}

impl<'a, T> Vector for &'a VectorImplementation1<T> {
    type Item = &'a T;
    type Iter = std::slice::Iter<'a, T>;

    fn iter(&self) -> Self::Iter {
        self.numbers.iter()
    }
}

fn main() {}

I see that the trait is implemented for a reference of struct, and it doesn't compile if I use only a struct. Can someone explain why?

1
I believe your question is answered by the answers of Is there any way to return a reference to a variable created in a function? and/or How do I write an iterator that returns references to itself?. If you disagree, please edit your question to explain the differences. Otherwise, we can mark this question as already answered.Shepmaster
TL;DR — If you took self and did impl<T> Vector for VectorImplementation1<T>, then the vector would go out of scope and the reference could never be valid. If you took &self and did impl<T> Vector for VectorImplementation1<T>, then you'd be trying to tie the reference of the returned value to a lifetime you can't name.Shepmaster

1 Answers

0
votes

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.