# Lifetime elision opportunity

**URL:** <https://internals.rust-lang.org/t/lifetime-elision-opportunity/5529>\
**Category:** language design\
**Created:** [July 11, 2017, 11:41pm UTC](https://internals.rust-lang.org/t/lifetime-elision-opportunity/5529 "2017-07-11T23:41:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jethrogb](https://avatars.discourse-cdn.com/v4/letter/j/43a26b/32.png) [@jethrogb](https://internals.rust-lang.org/u/jethrogb)\
**Post date:** [July 11, 2017, 11:41pm UTC](https://internals.rust-lang.org/t/lifetime-elision-opportunity/5529/1 "2017-07-11T23:41:45Z")

</div>

This code does not compile:

```rust
fn range_intersect(mut a: &Range<usize>, mut b: &Range<usize>) -> Option<Range<usize>> {
    if a.start > b.start {
        std::mem::swap(&mut a, &mut b);
    }
    
    if b.start < a.end {
        Some(b.start..a.end)
    } else {
        None
    }
}

```

This is because `a` and `b` have different lifetimes.

I fixed it like so:

```rust
fn range_intersect<'a>(mut a: &'a Range<usize>, mut b: &'a Range<usize>) -> Option<Range<usize>>

```

But then I was thinking, why do I need to annotate this? This could be automatic. In particular, if a `fn`'s output type has the static lifetime, all elided input lifetimes should be able to have the same lifetime (that of the function call). What do people think about implementing this relaxation of the elision rules?

---

<div class="post-metadata">

**Author:** ![jethrogb](https://avatars.discourse-cdn.com/v4/letter/j/43a26b/32.png) [@jethrogb](https://internals.rust-lang.org/u/jethrogb)\
**Post date:** [July 11, 2017, 11:42pm UTC](https://internals.rust-lang.org/t/lifetime-elision-opportunity/5529/2 "2017-07-11T23:42:13Z")

</div>

Unrelated note: another fun way to fix the above function without changing the signature:

```rust
fn range_intersect(&ref a: &Range<usize>, &ref b: &Range<usize>) -> Option<Range<usize>>

```

Except this doesn’t work because as @mbrubeck pointed out on IRC, `mut ref` bindings don’t exist.

---

<div class="post-metadata">

**Author:** ![mbrubeck](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mbrubeck/32/174_2.png) [@mbrubeck](https://internals.rust-lang.org/u/mbrubeck)\
**Post date:** [July 12, 2017, 12:37am UTC](https://internals.rust-lang.org/t/lifetime-elision-opportunity/5529/3 "2017-07-12T00:37:59Z")

</div>

I think it would be a breaking change to make _all_ elided input parameters equal in cases like this, since some of them might appear in invariant types, and that would change the meaning of the function signature.

Even for your example, it’s breaking in a lesser way, because `range_intersect` with elided lifetimes would no longer implement the `for<'a, 'b> Fn<(&'a Range<usize>, &'b Range<usize>)>` trait.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [March 25, 2019, 8:28am UTC](https://internals.rust-lang.org/t/lifetime-elision-opportunity/5529/4 "2019-03-25T08:28:45Z")

</div>

Closed on March 25, 2019, 8:28am UTC

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