# Lifetime elision with only the return type elided

**URL:** <https://internals.rust-lang.org/t/lifetime-elision-with-only-the-return-type-elided/4446>\
**Category:** Uncategorized\
**Created:** [December 8, 2016, 12:33pm UTC](https://internals.rust-lang.org/t/lifetime-elision-with-only-the-return-type-elided/4446 "2016-12-08T12:33:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bluss](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bluss/32/2264_2.png) [@bluss](https://internals.rust-lang.org/u/bluss)\
**Post date:** [December 8, 2016, 12:33pm UTC](https://internals.rust-lang.org/t/lifetime-elision-with-only-the-return-type-elided/4446/1 "2016-12-08T12:33:42Z")

</div>

I am surprised that the following code compiles, because the return type of `new` has the lifetime elided (while the input variable are explicit).

```rust
struct Lexer;

struct LexerIter<'a> {
    lexer: &'a Lexer,
}

impl<'a> LexerIter<'a> {
    pub fn new(lexer: &'a Lexer) -> LexerIter {
        LexerIter {
            lexer: lexer,
        }
    }
}

fn main() { }

```

I don’t see why this should compile. The naive equivalent (adding lifetime parameters) would have me go `pub fn new<'b>(lexer: &'a Lexer) -> LexerIter<'b>`, which is a type error. Am I missing something, or what is this strange case?

---

<div class="post-metadata">

**Author:** ![eddyb](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/eddyb/32/8411_2.png) [@eddyb](https://internals.rust-lang.org/u/eddyb)\
**Post date:** [December 8, 2016, 12:57pm UTC](https://internals.rust-lang.org/t/lifetime-elision-with-only-the-return-type-elided/4446/2 "2016-12-08T12:57:40Z")

</div>

This is a bit subtle, but, `fn new(_: &Lexer)` is not “elision” or at least not _the same kind_ as that found in the return type.

Instead, missing lifetimes in argument types get substituted with anonymous (late-bound) lifetime parameters, so these are all equivalent:

```rust
fn foo(_: &i32) -> R {...}
fn foo<'a>(_: &'a i32) -> R {...}
fn foo<'b>(_: &'b i32) -> R {...}

```

The return type lifetime elision, however, looks for more or less (`self` is a bit special) one lifetime in the argument types, and uses that lifetime in _all_ locations where a lifetime is missing. That is, `R` above can have any number of lifetime parameters and they all get the same lifetime from the reference to `i32`.

---

<div class="post-metadata">

**Author:** ![bluss](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bluss/32/2264_2.png) [@bluss](https://internals.rust-lang.org/u/bluss)\
**Post date:** [December 8, 2016, 1:12pm UTC](https://internals.rust-lang.org/t/lifetime-elision-with-only-the-return-type-elided/4446/3 "2016-12-08T13:12:32Z")

</div>

Thanks for elaborating! I had completely missed that it could be only partway explicit in where a parameter is used. It allows us to write Rust that is extra confusing:

```rust
fn pos<'x>(&'x self) -> &usize;

```

---

<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:** [December 8, 2016, 5:24pm UTC](https://internals.rust-lang.org/t/lifetime-elision-with-only-the-return-type-elided/4446/4 "2016-12-08T17:24:31Z")

</div>

I wouldn’t say that’s particularly confusing. Since the lifetime needs to be bound to an input (unless explicitly 'static) there’s really only one lifetime it could be here.

---

<div class="post-metadata">

**Author:** ![bluss](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bluss/32/2264_2.png) [@bluss](https://internals.rust-lang.org/u/bluss)\
**Post date:** [March 25, 2019, 8:27am UTC](https://internals.rust-lang.org/t/lifetime-elision-with-only-the-return-type-elided/4446/5 "2019-03-25T08:27:28Z")

</div>

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

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