Given the following code:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=0649baa344cf71ca09719ebebe1bf5d2
pub struct Foo;
pub struct Bar;
impl From<&Foo> for Bar {
fn from(_: &Foo) -> Bar { Bar }
}
pub fn fails<E>(e: E) -> Bar where &E: Into<Bar> {
(&e).into()
}
The current output is:
error[[E0637]](https://doc.rust-lang.org/stable/error-index.html#E0637): `&` without an explicit lifetime name cannot be used here
--> src/lib.rs:8:36
|
8 | pub fn fails<E>(e: E) -> Bar where &E: Into<Bar> {
| ^ explicit lifetime name needed here
error[[E0310]](https://doc.rust-lang.org/stable/error-index.html#E0310): the parameter type `E` may not live long enough
--> src/lib.rs:8:40
|
8 | pub fn fails<E>(e: E) -> Bar where &E: Into<Bar> {
| ^^^^^^^^^ ...so that the reference type `&'static E` does not outlive the data it points at
|
help: consider adding an explicit lifetime bound...
|
8 | pub fn fails<E: 'static>(e: E) -> Bar where &E: Into<Bar> {
| +++++++++
Ideally the output should suggest adding a HRTB like this:
pub fn works<E>(e: E) -> Bar where for<'e> &'e E: Into<Bar> {
(&e).into()
}
TBH, HRTB are a very obscure feature to me, and it is not obvious in which circumstances I need to use them. This is one of those, and the compiler should suggest this to me.
I think there are quite some usecases when you have a Into / From that works with a reference, and you want to write code that is generic over that Into / From type.
Given the following code:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=0649baa344cf71ca09719ebebe1bf5d2
The current output is:
Ideally the output should suggest adding a HRTB like this:
TBH, HRTB are a very obscure feature to me, and it is not obvious in which circumstances I need to use them. This is one of those, and the compiler should suggest this to me.
I think there are quite some usecases when you have a
Into/Fromthat works with a reference, and you want to write code that is generic over thatInto/Fromtype.