简体   繁体   English

生存期删除的第三条规则是否捕获了结构实现的所有情况?

[英]Does the third rule of lifetime elision capture all cases for struct implementations?

The third rule of lifetime elision says 终身淘汰的第三条规则说

If there are multiple input lifetime parameters, but one of them is &self or &mut self because this is a method, then the lifetime of self is assigned to all output lifetime parameters. 如果有多个输入生存期参数,但是其中一个是&self&mut self因为这是一种方法,则将self的生存期分配给所有输出生存期参数。 This makes writing methods much nicer. 这使编写方法变得更好。

Here is the tutorial describing what happened for this function 这是描述此函数发生情况的教程

fn announce_and_return_part(&self, announcement: &str) -> &str

There are two input lifetimes, so Rust applies the first lifetime elision rule and gives both &self and announcement their own lifetimes. 输入生命周期有两个,因此Rust会应用第一个生命周期省略规则,并给&selfannouncement自己的生命周期。 Then, because one of the parameters is &self , the return type gets the lifetime of &self , and all lifetimes have been accounted for. 然后,由于参数之一是&self ,返回类型将获得&self的生存期,并且所有生存期均已考虑在内。

We can show that all the lifetimes are not accounted for since it is possible that announcement will have a different lifetime than &self : 我们可以证明并未考虑所有生命周期,因为announcement生命周期可能与&self不同:

struct ImportantExcerpt<'a> {
    part: &'a str,
}

impl<'a> ImportantExcerpt<'a> {
    fn announce_and_return_part(&self, announcement: &str) -> &str {
        println!("Attention please: {}", announcement);
        announcement
    }
}

fn main() {
    let i = ImportantExcerpt { part: "IAOJSDI" };
    let test_string_lifetime;

    {
        let a = String::from("xyz");
        test_string_lifetime = i.announce_and_return_part(a.as_str());
    }
    println!("{:?}", test_string_lifetime);   
}

The lifetime of announcement is not as long as &self , so it is not correct to associate the output lifetime to &self , shouldn't the output lifetime be associated to the longer of the input? 的寿命announcement是不是只要&self ,所以它是不正确的关联的输出一辈子&self ,不应该输出寿命相关联,以输入的更久?

Why is the third rule of lifetime elision a valid way to assign output lifetime? 为什么寿命省略的第三条规则是分配输出寿命的有效方法?

No, the elision rules do not capture every possible case for lifetimes. 不可以, 省略规则不能一生捕获所有可能的情况。 If they did, then there wouldn't be any elision rules, they would be the only rules and we wouldn't need any syntax to specify explicit lifetimes. 如果它们这样做,那么就不会有任何省略规则,它们将是唯一的规则,并且我们不需要任何语法来指定显式生存期。

Quoting from the documentation you linked to, emphasis mine: 引用链接到的文档后,重点是:

The patterns programmed into Rust's analysis of references are called the lifetime elision rules. Rust进行的参考分析中编程的模式称为寿命省略规则。 These aren't rules for programmers to follow; 这些不是程序员要遵循的规则。 the rules are a set of particular cases that the compiler will consider, and if your code fits these cases, you don't need to write the lifetimes explicitly . 规则是编译器将考虑的一组特殊情况,如果您的代码适合这些情况,则无需显式编写生存期

The elision rules don't provide full inference: if Rust deterministically applies the rules but there's still ambiguity as to what lifetimes the references have, it won't guess what the lifetime of the remaining references should be . 省略规则不能提供完整的推论:如果Rust确定性地应用规则,但是在引用的生存期方面仍然存在歧义,它不会猜测其余引用的生存期应该是多少 In this case, the compiler will give you an error that can be resolved by adding the lifetime annotations that correspond to your intentions for how the references relate to each other. 在这种情况下,编译器会给您一个错误,可以通过添加与您打算如何将引用相互关联的生命周期注释来解决该错误。


The lifetime of announcement is not as long as &self , so it is not correct to associate the output lifetime to &self 的寿命announcement是不是只要&self ,所以它是不正确的输出一辈子的关联&self

Why is the third rule of lifetime elision a valid way to assign output lifetime? 为什么寿命省略的第三条规则是分配输出寿命的有效方法?

"correct" is probably not the right word to use here. “正确”可能不是此处使用的正确词。 What the elision rules have done is a valid way, it just doesn't happen to be what you might have wanted. 省略规则所做的是一种有效的方法,但这恰好不是您可能想要的。

shouldn't the output lifetime be associated to the longer of the input? 输出寿命不应该与更长的输入关联吗?

Yes, that would be acceptable for this example, it's just not the most common case, so it's not what the elision rules were aimed to do. 是的,对于本示例来说,这是可以接受的,这不是最常见的情况,因此,这并不是选举规则的目标。

See also: 也可以看看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM