更清晰的lifetime描述

Open Beginner friendly
#1,592 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
65/100
Issue type
Documentation
Clarity
Clearly specified
Activity status
Stale
Tech stack
rust
Domain
documentation

Research direction

Open basic/lifetime.md and locate chapter 2, section 10, especially the explanation of the announce_and_return_part example. Compare the current explanation of the 'a and 'b relationship with the proposed wording, then revise it so the promised return lifetime and possible dangling-reference concern are clear to beginners. Done means the lifetime explanation is more understandable and remains consistent with the example.

Written by the indexing model from the issue text.

Description

在书中第二章第10小节(对应basic/lifetime.md),有一段描述如下:

在结束这块儿内容之前,再来做一个有趣的修改,将方法返回的生命周期改为'b

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

此时,编译器会报错,因为编译器无法知道 'a'b 的关系。 &self 生命周期是 'a,那么 self.part 的生命周期也是 'a,但是好巧不巧的是,我们手动为返回值 self.part 标注了生命周期 'b,因此编译器需要知道 'a'b 的关系。

有一点很容易推理出来:由于 &'a self 是被引用的一方,因此引用它的 &'b str 必须要活得比它短,否则会出现悬垂引用。因此说明生命周期 'b 必须要比 'a 小,只要满足了这一点,编译器就不会再报错:

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

Bang,一个复杂的玩意儿被甩到了你面前,就问怕不怕?

其实作为一个初学者,我在看到这一小段描述是充满困惑的:

有一点很容易推理出来:由于 &'a self 是被引用的一方,因此引用它的 &'b str 必须要活得比它短,否则会出现悬垂引用。因此说明生命周期 'b 必须要比 'a 小,只要满足了这一点,编译器就不会再报错:

后面查了一些资料才理解了这段要表达的含义。我在想是不是可以改成下面这种描述,会减少一些初学者的困惑:

因为announce_and_return_part的函数签名承诺的返回值的生命周期是&'b str,所以&'b str的生命周期必须小于等于&'a str,程序才不会报错(设想一下,如果'b > 'a,函数实际返回了'a,但调用者把这个返回值当成'b使用,那么就会出现'a被释放了,调用者还在使用的情况)

Dominant language
Rust
Stars
30.9k
Forks
2.6k
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from sunface/rust-course

All issues in sunface/rust-course

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.