# Unconstrained lifetime parameter for impl

**URL:** <https://users.rust-lang.org/t/unconstrained-lifetime-parameter-for-impl/27995>\
**Category:** help\
**Created:** [May 6, 2019, 2:55pm UTC](https://users.rust-lang.org/t/unconstrained-lifetime-parameter-for-impl/27995 "2019-05-06T14:55:36Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![amarao](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/amarao/32/11182_2.png) [@amarao](https://users.rust-lang.org/u/amarao)\
**Post date:** [May 6, 2019, 2:55pm UTC](https://users.rust-lang.org/t/unconstrained-lifetime-parameter-for-impl/27995/1 "2019-05-06T14:55:37Z")

</div>

I checked Rust Book and found they never discussed this error in examples. Moreover, they never talked about implementing lifetimes with associated types at the same time.

I'm beginner and I feel lost. I want to return 'Pixel' from iterator for Canvas with mutable reference to 'next' pixel.

This is my current code, and it does not work with 'unconstrained lifetime parameter'. I have no idea where and what to read. Please, help.

My code:

```
struct Pixel<'img> {
    value: &'img mut u32,
    i: usize,
}

struct Canvas {
    img: Vec<u32>,
    i: usize,
    max_i: usize
}

impl<'a> Iterator for Canvas {
    type Item=Pixel<'a>;
    fn next(&'a mut self) -> Pixel<'a>{
        if self.i >= self.max_i{
            return None
        }
        let retval = Pixel{value: &mut self.img[self.i], i: self.i};
        self.i+=1;
        Some(retval)
    }
}

fn main(){
    let mut canvas = Canvas{img:vec![0;9], i: 0, max_i:3};
    for pixel in canvas {
        * pixel.value = 3 as u32;
    }
}

```

---

<div class="post-metadata">

**Author:** ![RustyYato](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/rustyyato/32/49247_2.png) [@RustyYato](https://users.rust-lang.org/u/RustyYato)\
**Post date:** [May 6, 2019, 3:30pm UTC](https://users.rust-lang.org/t/unconstrained-lifetime-parameter-for-impl/27995/2 "2019-05-06T15:30:45Z")

</div>

This is something you can't do in Rust yet, what you are trying to make is a streaming iterator, but this can't be done without generic associated types (GAT). The closet alternative is to use the streaming iterator crate

[https://crates.io/crates/streaming-iterator](https://crates.io/crates/streaming-iterator)

---

<div class="post-metadata">

**Author:** ![masonk](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/masonk/32/5834_2.png) [@masonk](https://users.rust-lang.org/u/masonk)\
**Post date:** [May 6, 2019, 3:34pm UTC](https://users.rust-lang.org/t/unconstrained-lifetime-parameter-for-impl/27995/3 "2019-05-06T15:34:10Z")

</div>

As KrishnaSannasi said, this is the [GATs](https://github.com/rust-lang/rfcs/blob/master/text/1598-generic_associated_types.md) problem.

To dig in a little more: How long does `'a` live? What you want to express is that `Pixel<'a>` lives no longer than `&'a mut self`, but that relationship would have to be in the trait definition (because it would have to be provably true about all impls of the trait), and it isn't there. It can't be there, because right now it is not possible to use type parameters in associated types (they cannot be generic). And, recall that lifetime parameters are type parameters.

See also this [SO answer](https://stackoverflow.com/questions/30422177/how-do-i-write-an-iterator-that-returns-references-to-itself).

---

<div class="post-metadata">

**Author:** ![Yandros](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/yandros/32/8136_2.png) [@Yandros](https://users.rust-lang.org/u/Yandros)\
**Post date:** [May 6, 2019, 4:58pm UTC](https://users.rust-lang.org/t/unconstrained-lifetime-parameter-for-impl/27995/4 "2019-05-06T16:58:56Z")

</div>

Just so you know, in your case you can circumvent your issue by using functional style:

```rust
#![deny(elided_lifetimes_in_paths)]

struct Pixel<'img> {
    value: &'img mut u32,
    i: usize,
}

struct Canvas {
    imgs: Vec<u32>,
    max_i: usize,
}

impl Canvas {
    pub
    fn iter_mut<'iter> (
        self: &'iter mut Self,
    ) -> impl Iterator<Item = Pixel<'iter>>
    {
        self.imgs[.. self.max_i]
            .iter_mut()
            .enumerate()
            .map(|(i, value)| Pixel { value, i })
    }
}

fn main ()
{
    let mut canvas = Canvas {
        imgs: vec![0; 9],
        max_i: 3,
    };
    for pixel in canvas.iter_mut() {
        *pixel.value = 3;
    }
}

```

---

<div class="post-metadata">

**Author:** ![amarao](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/amarao/32/11182_2.png) [@amarao](https://users.rust-lang.org/u/amarao)\
**Post date:** [May 6, 2019, 6:15pm UTC](https://users.rust-lang.org/t/unconstrained-lifetime-parameter-for-impl/27995/5 "2019-05-06T18:15:45Z")

</div>

Thank you everyone for answers. I really appreciate the help I got. The lifetimes logic with traits on types is still to hard for me, but I'll try harder.

---

<div class="post-metadata">

**Author:** ![RustyYato](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/rustyyato/32/49247_2.png) [@RustyYato](https://users.rust-lang.org/u/RustyYato)\
**Post date:** [May 6, 2019, 6:17pm UTC](https://users.rust-lang.org/t/unconstrained-lifetime-parameter-for-impl/27995/6 "2019-05-06T18:17:25Z")

</div>

Keep at it, lifetimes are hard and will take some time to understand!

---

<div class="post-metadata">

**Author:** ![amarao](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/amarao/32/11182_2.png) [@amarao](https://users.rust-lang.org/u/amarao)\
**Post date:** [May 6, 2019, 7:07pm UTC](https://users.rust-lang.org/t/unconstrained-lifetime-parameter-for-impl/27995/7 "2019-05-06T19:07:51Z")

</div>

@Yandros, I'm trying to study your code, but I can't find any documentation for `elided_lifetimes_in_paths`. Can you point me to documentation for it? The name itself is somewhat self-explanatory (elided lifetimes) but I can't understand what is 'path' here.

---

<div class="post-metadata">

**Author:** ![Yandros](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/yandros/32/8136_2.png) [@Yandros](https://users.rust-lang.org/u/Yandros)\
**Post date:** [May 6, 2019, 8:14pm UTC](https://users.rust-lang.org/t/unconstrained-lifetime-parameter-for-impl/27995/8 "2019-05-06T20:14:35Z")

</div>

I've answered in [another thread](https://users.rust-lang.org/t/what-is-the-elided-lifetimes-in-paths-lint-for/28005/2), since it is a little bit off-topic here and my answer has ended up a little bit longer than expected 😅

---

<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:** [August 4, 2019, 8:14pm UTC](https://users.rust-lang.org/t/unconstrained-lifetime-parameter-for-impl/27995/9 "2019-08-04T20:14:41Z")

</div>

Closed on August 4, 2019, 8:14pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
