如何在AsRef上使用生命周期 [英] How to use the lifetime on AsRef
本文介绍了如何在AsRef上使用生命周期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我很难理解下面的代码如何使用生命周期.我知道明确的生存期对于帮助编译器了解何时可以保存/释放数据是必需的,但是在这种特殊情况下,url.serialize()
会生成一个匿名字符串,我不确定如何解决此问题.
I'm having a hard time understanding how to use lifetimes with the code below. I understand that explicit lifetimes are necessary to aid the compiler in understanding when it can hold/release data but in this particular case, url.serialize()
generates an anonymous string and I'm not really sure how to resolve this issue.
impl AsRef<str> for RequestUri {
#[inline]
fn as_ref(&self) -> &str {
match self {
&RequestUri::AbsoluteUri(ref url) => url.serialize().as_ref()
}
}
}
推荐答案
查看全文