# Improve HRTB compatibility for "lifetime not general enough" cases, or provide better help/notes + update language tutorials

**URL:** <https://internals.rust-lang.org/t/improve-hrtb-compatibility-for-lifetime-not-general-enough-cases-or-provide-better-help-notes-update-language-tutorials/19127>\
**Category:** traits working group\
**Created:** [July 11, 2023, 9:54am UTC](https://internals.rust-lang.org/t/improve-hrtb-compatibility-for-lifetime-not-general-enough-cases-or-provide-better-help-notes-update-language-tutorials/19127 "2023-07-11T09:54:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sunshine](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/sunshine/32/10976_2.png) [@Sunshine](https://internals.rust-lang.org/u/Sunshine)\
**Post date:** [July 11, 2023, 9:54am UTC](https://internals.rust-lang.org/t/improve-hrtb-compatibility-for-lifetime-not-general-enough-cases-or-provide-better-help-notes-update-language-tutorials/19127/1 "2023-07-11T09:54:53Z")

</div>

Continuing the discussion from [Idea: aliasing the `'static` lifetime for lifetime parameters / trait bounds, e.g., `'auto`](https://internals.rust-lang.org/t/idea-aliasing-the-static-lifetime-for-lifetime-parameters-trait-bounds-e-g-auto/19117/8):

It became clear that there're misunderstandings included in my original post, and after we discussed about that, I'd extract the root issue as @CAD97 had pointed out:

> [@Idea: aliasing the \`'static\` lifetime for lifetime parameters / trait bounds, e.g., \`'auto\`](https://internals.rust-lang.org/t/idea-aliasing-the-static-lifetime-for-lifetime-parameters-trait-bounds-e-g-auto/19117/8):
>
> If there's a "root" issue here, it's that std provides
> 
> ```rust
> impl<'a> From<String> for Cow<'a, str>
> // and
> impl<'a> From<&'a str> for Cow<'a, str>
> // but not
> impl<'a> From<&'static str> for Cow<'a, str>
> 
> // without overlapping impls, it'd be spelled
> impl<'short, 'long: 'short> From<&'long str> for Cow<'short, str>
> // but this would cause a lot of inference breakage
> // as Cow::from("str") would have multiple candidates
> 
> ```

So basically when I wrote:

```rust
fn my_fn<T: for<'a> Into<Cow<'a, str>>>(param: T) {}

```

so that I can pass either a `&'static str` or a `String` as a `param`, in the former case the compiler complained:

```rust
error: implementation of `From` is not general enough
  --> src\**
   |
** | my_fn("string literal");
   | ^^^^^^^^^^^^^^^^^^^^^^^ implementation of `From` is not general enough
   |
   = note: `From<&str>` would have to be implemented for the type `Cow<'0, str>`, for any lifetime `'0`...
   = note: ...but `From<&'1 str>` is actually implemented for the type `Cow<'1, str>`, for some specific lifetime `'1`

```

which is really not helpful, since a string literal (namely `&'static str`) could be converted to a `Cow<'a, str>` with any lifetime `'a`, if it's just converted to a `Cow<'static, str>` and then coerced to a `Cow<'a, str>`.

Fortunately for this specific case (I just need to support `&'static str` instead of any `&'a str`), I can write:

```rust
fn my_fn<T: Into<Cow<'static, str>>>(param: T) {}

```

instead, and the code works.

But, if I wrote:

```rust
fn my_fn<T: Into<Cow<str>>>(param: T) {}

```

in the first place, the compiler would hint:

```rust
error[E0106]: missing lifetime specifier
  --> src\**
   |
** | fn my_fn<T: Into<Cow<str>>>(param: T) {}
   | ^ expected named lifetime parameter
   |
   = note: for more information on higher-ranked polymorphism, visit https://doc.rust-lang.org/nomicon/hrtb.html
help: consider making the bound lifetime-generic with a new `'a` lifetime
   |
** | fn my_fn<T: for<'a> Into<Cow<'a, str>>>(param: T) {}
   | +++++++ +++
help: consider introducing a named lifetime parameter
   |
** | fn my_fn<'a, T: Into<Cow<'a, str>>>(param: T) {}
   | +++ +++

```

So during the whole process, I (as a Rust beginner) had got no guide leading me to the solution to the HRTB "lifetime not general enough" error (when it seems the lifetime in the implementation _is_ actually "general enough", or ready for use).

The ideal fix would be to eliminate the "lifetime not general enough" error in similar cases by improving HRTB compatibility.

If that could not be done (soon enough), then at least I think a better guide is needed to help people figure out that replacing `for<'a> Into<Cow<'a, str>>` with `Into<Cow<'static, str>>` _might_ fix the error, as mentioned by @CAD97:

> [@Idea: aliasing the \`'static\` lifetime for lifetime parameters / trait bounds, e.g., \`'auto\`](https://internals.rust-lang.org/t/idea-aliasing-the-static-lifetime-for-lifetime-parameters-trait-bounds-e-g-auto/19117/8):
>
> Any time you're expressing a function signature of the shape `fn() -> for<'a> &'a T`, you want to say `fn() -> &'static T` instead. This holds for any "covariant" lifetime (most of them).

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/steffahn/32/13288_2.png) [@steffahn](https://internals.rust-lang.org/u/steffahn)\
**Post date:** [January 1, 2025, 9:55am UTC](https://internals.rust-lang.org/t/improve-hrtb-compatibility-for-lifetime-not-general-enough-cases-or-provide-better-help-notes-update-language-tutorials/19127/2 "2025-01-01T09:55:07Z")

</div>

Closed on January 1, 2025, 9:55am UTC

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