hi! I am trying to understand this example from: dyn Trait implementations - Learning Rust
Does the trait bound on Iteratable mean that Self should impl IntoIterator, and, if there's any lifetime with Self, then that lifetime should be the same as the lifetime on items yielded by IntoIterator?
trait Iterable
where
for<'a> &'a Self: IntoIterator<
Item = &'a <Self as Iterable>::Borrow,
>,
{
type Borrow;
fn iter(&self) -> Box<dyn Iterator<Item = &Self::Borrow> + '_> {
Box::new(self.into_iter())
}
}
impl<I: ?Sized, Borrow> Iterable for I
where
for<'a> &'a Self: IntoIterator<Item = &'a Borrow>,
{
type Borrow = Borrow;
}
fn example(v: Vec<String>) {
// This compiles, demonstrating that we can create `dyn Iterable`
// (i.e. the trait is object safe and `v` can be coerced)
let dt: &dyn Iterable<Borrow = String> = &v;
// But this gives an error as `&dyn Iterable` doesn't meet the trait
// bound, and thus `dyn Iterable` does not implement `Iterable`!
for item in dt.iter() {
println!("{item}");
}
}