< 返回版块

ankoGo 发表于 2024-04-24 08:37

"Lifetime annotations don’t change how long any of the references live. Rather, they describe the relationships of the lifetimes of multiple references to each other without affecting the lifetimes. " 请问大伙是怎么理解这段文字的?特别是第一句话!

评论区

写评论
作者 ankoGo 2024-04-25 18:45

数据肯定是有生命周期可言的,但是我这里的“生命周期”更加侧重于引用的生命周期,比如mut stu,我关心的并不是这个value是否还有效,而是关心stu是否还在mut可变借用中

作者 ankoGo 2024-04-25 18:43

回复下TinusgragLin: 我的意思其实是希望官方改善相应的文档,好让入门的人看到这句话的时候不会有其他层面的误解,比如我就会理解成为“如何我如何修改生命周期的标注,任何的自身引用或者其他引用的生命周期都不会被修改”,这很明显就不正确了,我的例子仅仅是修改了一个标注,就导致了mut stu这个变量的引用的生命周期发生了改变,进而反驳了官网的这句话,我的意思是这个!

作者 ankoGo 2024-04-25 18:39

“这里 -> &'a str 表示返回值的生命周期 ≥ 'a,其实由于"anko1"生命周期是 'static,这里用 'a 或 'b,这个函数本身都是正确的。” 我不是说这个函数正确与否,我说的是mut stu的引用已经被结束,只因为 'a 而非 'b,这就侧面说明了,我只需要更改返回值的生命周期标注,就可以做到更改mut stu的引用的生命周期,所以官网文档表述的“注解无法改变引用的生命周期”这句话是不妥当的额,我是为了说了官网的这句话放到新手的眼里会存在歧义,而不是说这个函数的标注正确与否的问题!我的意思是存在歧义,不同的人对这句话的理解是完全不一样的,而这句话我个人认为是存在歧义的,它的表述需要更多细节描述和例子,单纯从文字上来看,这句话不同的人的理解不一样,这就会造成很大的误解!

TinusgragLin 2024-04-25 18:34

the lifetime of some references changed because I changed the labeling.

I think what the rust book is trying to say is that: references are really just memory address data, so just like any data in your program, they "live" as long as they are used and their backing storage are not reclaimed for other uses.

And at the end of the day, you care more about:

  1. the validity of a reference, i.e. whether the pointed-to data is still valid,
  2. whether it is safe to read/modify the data via the reference, i.e. whether a data race can happen.

this is where lifetime annotations come in: the lifetime annotation attached to a reference is about the validity of the pointed-to data and the guarantee of data-race free.

So what &'a T actually means is that: for the duration of 'a, the pointed-to data of this reference is guaranteed to be valid and no one else is modifying it. This is a bold guarantee to be made, as proven by numerous memory bugs found in C/C++ programs. By making this guarantee explicit, the hard work of proving is left to the compiler. This is exactly why we need lifetime annotation in Rust in the first place.

asuper 2024-04-25 16:27

我觉得没有问题,生命周期标注是一种约束,是对编译器的保证。

    fn mutate_and_share<'b>(&'b mut self) -> &'a str/*** mark A ***/ {
        self.name= "anko1";
        let n=self.name;
        n
    }

这里 -> &'a str 表示返回值的生命周期 ≥ 'a,其实由于"anko1"生命周期是 'static,这里用 'a 或 'b,这个函数本身都是正确的。

所以main()函数中报错,是由于标注了 fn mutate_and_share<'b>(&'b mut self) -> &'b str 后引发了 可变引用后使用不可变引用 的编译器检查错误,这个没有问题的。

作者 ankoGo 2024-04-25 12:45

不觉得,参考这段代码:

// Thank you so much to all the big guys for taking the time to come out and answer my concerns,
// please discard all the information I have stated above and then focus on this next example I have given,
// study the following example carefully and hopefully you will be able to see what exactly CHANGE means
// as I understand it in my mind! I am not a native English speaker, so I may not be able to express myself well,
// so I hope you will bear with me and thank you again, brothers!
#[derive(Debug)]
struct Student<'a> {
    name: &'a str,
}

impl<'a> Student<'a> {
    fn mutate_and_share<'b>(&'b mut self) -> &'a str/*** mark A ***/ {
        self.name= "anko1";
        let n=self.name;
        n
        // the lifetime of "mut stu" here is over.so next content "stu.share()" is allowed by the compiler.
        // "n" is return back to "loan",but the lifetime of "loan" is different with the "mut stu".
        // Now if I change the lifetime annotation syntax 'a to 'b at mark A above,"stu.share()" will panic.
        // why?why?why?Pay attention. Here's the kicker.
        // This is because at this point I've changed the lifetime of the "mut stu" instance via the lifecycle marker,
        // causing it("mut stu") to not be released before calling this function "stu.share()",so it panic.
        // That's what I mean by "change".
        // That's what I mean by "change".
        // That's what I mean by "change".
        // Pay attention. Here's the kicker.
        // The change here is exactly the same as I understand the change in this sentence("Lifetime annotations
        // don’t change how long any of the references live. ") from the documentation on the official website
    }
    fn share(&'a self) {}
}

fn main() {
    let mut stu = Student { name: "anko" };
    let loan = stu.mutate_and_share();
    stu.share();
    println!("{:?}", loan);
}

As you can see from the above example: because I've changed the lifetime annotation of the return value, the lifetime of a reference will change as a result. So there is an ambiguity in this sentence on the official website, and that's where the ambiguity shows up! Can't the lifetime of a reference be changed? No, the lifetime of some references changed because I changed the labeling. That's how I understood the word “change”, and it misled me for years!

asuper 2024-04-24 09:31

没有问题的,生命周期标注不会改变引用的生存期。annotations,字典里写的是注释、注解,中文一般翻译为“标注”,顾名思义,他不应该影响代码行为,只是用于提示编译器的,用于生存期检查。

1 共 7 条评论, 1 页