为什么我可以在一种情况下详细介绍返回类型的寿命,而另一种情况需要明确选择寿命?

发布于 2025-02-04 06:54:38 字数 1948 浏览 2 评论 0 原文

在这里,我必须给出 app 全生寿期,并且不能( app<'_> ):

struct App<'a> {
    items: StatefulList<'a, (&'a str, &'a str, usize)>,
}

impl App<'_> {
    fn new<'a>(items: &'a Vec<(&'a str, &'a str, usize)>) -> App<'a> {
        App {
            items: StatefulList::with_items(items),
        }
    }
}

如果我仅指定外部的寿命,它也有效vec

fn new<'a>(items: &'a Vec<(&str, &str, usize)>) -> App<'a>

如果我尝试将其列出,它给出( Playground ):

error[E0106]: missing lifetime specifier
  --> src/lib.rs:18:53
   |
18 |     fn new(items: &Vec<(&str, &str, usize)>) -> App<'_> {
   |                   -------------------------         ^^ expected named lifetime parameter
   |
   = help: this function's return type contains a borrowed value, but the signature does not say which one of `items`'s 3 lifetimes it is borrowed from
help: consider introducing a named lifetime parameter
   |
18 |     fn new<'a>(items: &'a Vec<(&str, &str, usize)>) -> App<'a> {
   |           ++++         ++                                  ~~

但是,在这里 with_items 方法我可以愉快地终身:

struct StatefulList<'a, T> {
    state: ListState,
    items: &'a Vec<T>,
}

impl<T> StatefulList<'_, T> {
    fn with_items(items: &Vec<T>) -> StatefulList<'_, T> {
        StatefulList {
            state: ListState::default(),
            items,
        }
    }
}

为什么?

Here, I must give App a full lifetime and cannot elide it (App<'_>):

struct App<'a> {
    items: StatefulList<'a, (&'a str, &'a str, usize)>,
}

impl App<'_> {
    fn new<'a>(items: &'a Vec<(&'a str, &'a str, usize)>) -> App<'a> {
        App {
            items: StatefulList::with_items(items),
        }
    }
}

It also works if I only specify the lifetime for the outer Vec:

fn new<'a>(items: &'a Vec<(&str, &str, usize)>) -> App<'a>

If I try to elide it, it gives (playground):

error[E0106]: missing lifetime specifier
  --> src/lib.rs:18:53
   |
18 |     fn new(items: &Vec<(&str, &str, usize)>) -> App<'_> {
   |                   -------------------------         ^^ expected named lifetime parameter
   |
   = help: this function's return type contains a borrowed value, but the signature does not say which one of `items`'s 3 lifetimes it is borrowed from
help: consider introducing a named lifetime parameter
   |
18 |     fn new<'a>(items: &'a Vec<(&str, &str, usize)>) -> App<'a> {
   |           ++++         ++                                  ~~

However, here for with_items method I can happily elide the lifetime:

struct StatefulList<'a, T> {
    state: ListState,
    items: &'a Vec<T>,
}

impl<T> StatefulList<'_, T> {
    fn with_items(items: &Vec<T>) -> StatefulList<'_, T> {
        StatefulList {
            state: ListState::default(),
            items,
        }
    }
}

Why is that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一念一轮回 2025-02-11 06:54:38

编译器告诉您原因:

此函数的返回类型包含借用的值,但签名并未说出项目的3个寿命中的哪一种是从

借用的

从类型三个寿命>&amp; vec&lt;(&amp; str,str,usize)&gt; :&amp; /代码>。 lifetime Elision sulision ulision规则只能在返回类型时选择寿命有一个一个参数,或者如果有 self 参数,则在这种情况下它们使用其寿命。

The compiler is telling you why:

this function's return type contains a borrowed value, but the signature does not say which one of items's 3 lifetimes it is borrowed from

There are three lifetimes in the type &Vec<(&str, &str, usize)>: &'a Vec<(&'b str, &'c str, usize)>. The lifetime elision rules can only choose the lifetime for the return type when there is one parameter, or if there is a self parameter in which case they uses its lifetime.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文