admin管理员组文章数量:1122832
I've simplified my code to
mod fail {
use std::borrow::Cow;
pub struct C<'a>(Cow<'a, [&'a ()]>);
struct CWrapper<'a>(&'a C<'a>);
pub fn foo(c: &C) {
let _ = CWrapper(c);
}
}
mod ok_std_cow {
use std::borrow::Cow;
pub struct C<'a>(Cow<'a, [()]>);
struct CWrapper<'a>(&'a C<'a>);
pub fn foo(c: &C) {
let _ = CWrapper(c);
}
}
mod ok_my_cow {
enum MyCow<'a> {
Borrowed(&'a [&'a ()]),
Owned(Vec<&'a ()>),
}
pub struct C<'a>(MyCow<'a>);
struct CWrapper<'a>(&'a C<'a>);
pub fn foo(c: &C) {
let _ = CWrapper(c);
}
}
>> Playground
error: lifetime may not live long enough
--> src/lib.rs:8:26
|
7 | pub fn foo(c: &C) {
| - - let's call the lifetime of this reference `'1`
| |
| has type `&fail::C<'2>`
8 | let _ = CWrapper(c);
| ^ this usage requires that `'1` must outlive `'2`
|
help: consider introducing a named lifetime parameter
|
7 | pub fn foo<'a>(c: &'a C<'a>) {
| ++++ ++ ++++
Questions:
- Why does the
fail::foo
function emit error? - Could I fix it without introducing changes to the function signature of
foo
and the generic list ofstruct C
, and without reimplementingCow
?
本文标签: rustLifetime may not live long enough when Cow39s generic type contains lifetimeStack Overflow
版权声明:本文标题:rust - Lifetime may not live long enough when Cow's generic type contains lifetime - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736300334a1930776.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论