How do you deal with the virality of lifetime annotations in Rust?

64 points tigregalis

How do you deal with the virality of lifetime annotations in Rust?

This is a simple illustration of what I see as one of the only problems with the language:

mod old_code {
    struct HoldsHoldsHoldsString(HoldsHoldsString);
    struct HoldsHoldsString(HoldsString);
    struct HoldsString(String); // now I want to refactor this to use a &str
}

mod new_code {
    // struct HoldsString(&str); // <- won't compile:

    // 10 |     struct HoldsString(&str); // now I want to refactor this to a &str
    //    |                        ^ expected named lifetime parameter
    //    |
    // help: consider introducing a named lifetime parameter
    //    |
    // 10 |     struct HoldsString<'a>(&'a str); // now I want to refactor this to a &str
    //    |                       ++++  ++

    struct HoldsString<'a>(&'a str);

    // struct HoldsHoldsString(HoldsString); // <- won't compile:

    // 16 |     struct HoldsHoldsString(HoldsString);
    //    |                             ^^^^^^^^^^^ expected named lifetime parameter
    //    |
    // help: consider introducing a named lifetime parameter
    //    |
    // 16 |     struct HoldsHoldsString<'a>(HoldsString<'a>);
    //    |                            ++++            ++++

    struct HoldsHoldsString<'a>(HoldsString<'a>);

    // struct HoldsHoldsHoldsString(HoldsHoldsString); // <- won't compile:

    // 25 |     struct HoldsHoldsHoldsString(HoldsHoldsString);
    //    |                                  ^^^^^^^^^^^^^^^^ expected named lifetime parameter
    //    |
    // help: consider introducing a named lifetime parameter
    //    |
    // 25 |     struct HoldsHoldsHoldsString<'a>(HoldsHoldsString<'a>);
    //    |                                 ++++                 ++++

    struct HoldsHoldsHoldsString<'a>(HoldsHoldsString<'a>);
}

This is a 3-pronged question:

  1. How do you feel about this problem? (If you feel it is a problem at all)
  2. What solutions or workarounds do you have for this problem?
  3. Can Rust ever do anything, as a language, to "fix" this problem?