Is Lifetime Subtyping Obsolete?


#1

I just came across an issue, where The Book (even the Nightly version) presumably has been outdated by the furious pace of compiler development.

In Chapter 19.2 Advanced Lifetimes, the following code is presented in Listing 19-15:

struct Context<'s>(&'s str);

struct Parser<'c, 's> {
    context: &'c Context<'s>,
}

impl<'c, 's> Parser<'c, 's> {
    fn parse(&self) -> Result<(), &'s str> {
        Err(&self.context.0[1..])
    }
}

fn parse_context(context: Context) -> Result<(), &str> {
    Parser { context: &context }.parse()
}

The text following this example claims that this code is not compileable, because Lifetime Subtyping is needed in this case. Hereafter, it is stated that the following code using Lifetime subtyping would fix the problem:

struct Parser<'c, 's: 'c> {
    context: &'c Context<'s>,
}

I tried to reproduce the compiler error - without success ! The code without Lifetime Subtyping for Parser compiles and also runs perfectly, oviously the subtyping is no longer needed in this scenario.

Actually, the text explaining why subtyping is needed here looked pretty reasonable to me. The more surpising apprerrs to me, that this reasoning is obviously no longer correct.

Thus, i’m now wondering :

  • Why is Lifetime Subtyping no longer needed in this code ?

  • Is Lifetime Subtyping obsolete at all with the current compiler ?

  • If not: Could anybody point me to an (if possible, not too complex) example where it is stll needed ?

Again, thanks in advance for any help !


#2

Yes, we plan to remove this part of the book, it’s just stagnated while we decide if we should remove more :slight_smile: See the linked issues for some additional context.


#3

Hi Carol,
many thanks for pointing this out. Just in case… Do you know about any other Chapter of The Book that is outdated with Rust 2018 ?


#4

Our open pull requests and issues document what I’m aware of. Also note that the book rides the release trains, so the latest that’s available is at https://doc.rust-lang.org/nightly/book/.