About in-band lifetime

In the following code , 'a is an in-band lifetime , but how about 'b ? it is obvious that 'b is not a lifetime use , because there is no 'b declared in the scope.

fn main() {
  trait Tr<'x> {}
  struct U<'y>(&'y u8);  
  impl Tr<'a> for U<'b> {}
}

This code does not compile, it is meaningless. What do you mean by "in-band" lifetime anyway? That's not standard Rust terminology.

it compile. the formal term is argument lifetime.I found it indeed an in-band life time by the following test

fn main() {
  trait Tr<'x> {
    fn test(x:&'x u8);
  }
  struct U<'y>(&'y u8);  
  impl Tr<'a> for U<'b> {
    fn test(x:&'a u8) {
      let y : &'b u8=x;    // can use lifetime 'b
    }
  }
}

The unstable in_band_lifetimes feature introduces a short-hand where you do not have to declare lifetimes. The way to write what you suggested on stable Rust is the following:

impl<'a, 'b> Tr<'a> for U<'b> {}

There's no real difference between 'a and 'b here.

1 Like

Code like this is exactly why in_band_lifetimes should die in a fire.

That RFC combined several features that should have been evaluated independently. I'm glad we have '_ now, and that omitting lifetimes in types is now deprecated, but I see no good reason why implicitly declared lifetimes should be stabilized now.

3 Likes

so '_ is newer?

Yes, it was another part of the argument lifetimes RFC.

Alright, so it's unstable. When asking a question, you should probably mention that you are using unstable features, not everyone does/expects that by default.

+1.

1 Like

It was originally part of in-band lifetimes, but different parts of it got split off from it over time.

First, the '_ in function signatures was split into Tracking issue for `'_` · Issue #48469 · rust-lang/rust · GitHub and stabilized.

Then things like impl Debug for Foo<'_> was part of in-band lifetimes during the pre-edition FCP back in 2018, but then it was split into Tracking issue for lifetime elision for impl headers (feature impl_header_lifetime_elision) · Issue #15872 · rust-lang/rust · GitHub and stabilized.

Then the FCP for the rest of in_band_lifetimes didn't make progress, so was cancelled since it wasn't going to happen for the edition.

And then basically nothing else has happened about in-band lifetimes for three years, other than discussions about lints (that would apply with or without the unstable feature).

1 Like

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.