# Implement chain() for a Lending Iterator (GAT)

**URL:** <https://users.rust-lang.org/t/implement-chain-for-a-lending-iterator-gat/73608>\
**Category:** help\
**Created:** [March 30, 2022, 5:10am UTC](https://users.rust-lang.org/t/implement-chain-for-a-lending-iterator-gat/73608 "2022-03-30T05:10:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![AlastairHolmes](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/alastairholmes/32/26190_2.png) [@AlastairHolmes](https://users.rust-lang.org/u/AlastairHolmes)\
**Post date:** [March 30, 2022, 5:10am UTC](https://users.rust-lang.org/t/implement-chain-for-a-lending-iterator-gat/73608/1 "2022-03-30T05:10:48Z")

</div>

I am trying to implement an `Iterator::chain()`-like operation for a lending iterator (Using `#![feature(generic_associated_types)]`):

```rust
#![feature(generic_associated_types)]

pub trait LendingIterator {
	type Item<'a> where Self: 'a;

	fn next(&mut self) -> Option<Self::Item<'_>>;
}

```

To do this, similarly to the normal Iterator::chain(), I need a struct `Chain` that contains the two chained LendingIterators and impl's `LendingIterator`.

The problem I have is I need to specify the iterator types `A` and `B` having matching `Item` generic associated types, so I tried this:

```rust
pub struct Chain<A, B> {
	a: Option<A>,
	b: Option<B>,
}

impl<A, B> LendingIterator for Chain<A, B>
    where
        A: LendingIterator,
        B: for<'a> LendingIterator<Item<'a>=A::Item<'a>>
{
	type Item<'a> where Self: 'a = A::Item<'a>;

	fn next(&mut self) -> Option<Self::Item<'_>> {
        todo!()
	}
}

```

But I get:

```rust
error[E0311]: the parameter type `B` may not live long enough
  --> src\lib.rs:63:12
   |
63 | impl<A, B> LendingIterator for Chain<A, B> where A: LendingIterator, B: for<'a> LendingIterator<Item<'a>=A::Item<'a>> {
   | - ^^^^^^^^^^^^^^^ ...so that the type `B` will meet its required lifetime bounds...
   | |
   | help: consider adding an explicit lifetime bound...: `B: 'a`
   |
note: ...that is required by this bound
   | ^^^^ ...so that the type `B` will meet its required lifetime bounds

```

Of course the compiler's suggestions don't seem to work.

How can I add the constraint that `B` does have a long enough lifetime?

---

<div class="post-metadata">

**Author:** ![AlastairHolmes](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/alastairholmes/32/26190_2.png) [@AlastairHolmes](https://users.rust-lang.org/u/AlastairHolmes)\
**Post date:** [April 7, 2022, 7:44pm UTC](https://users.rust-lang.org/t/implement-chain-for-a-lending-iterator-gat/73608/2 "2022-04-07T19:44:17Z")

</div>

Turns out this is a bug in rust's GAT implementation. See this:

"[Zulip](https://rust-lang.zulipchat.com/#narrow/stream/122651-general/topic/.E2.9C.94.20GAT.20LendingIterator.3A.3Achain.20Issue/near/278018043)"

"[Zulip](https://rust-lang.zulipchat.com/#narrow/stream/213817-t-lang/topic/Understanding.20the.20motivations.20for.20GATs/near/269863728)"

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [July 6, 2022, 7:44pm UTC](https://users.rust-lang.org/t/implement-chain-for-a-lending-iterator-gat/73608/3 "2022-07-06T19:44:32Z")

</div>

Closed on July 6, 2022, 7:44pm UTC

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.
