更清晰的lifetime描述
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
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from sunface/rust-course
-
Difficulty 1/5 Under an hour Newbie friendliness 88/100
sunface/rust-course#1607 ·
-
Difficulty 1/5 Under an hour Newbie friendliness 75/100
sunface/rust-course#1604 · 2 reactions ·
-
有很多超链接内容无法显示 Open
Difficulty 3/5 1-2 days Newbie friendliness 45/100
sunface/rust-course#1647 ·
-
浮点数中的浮点数陷阱第2点例子没了 Open
Difficulty 2/5 1-3 hours Newbie friendliness 45/100
sunface/rust-course#1628 ·
-
文章内链接失效 Open
Difficulty 3/5 1-2 days Newbie friendliness 52/100
sunface/rust-course#1609 · 2 comments ·
All issues in sunface/rust-course
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
Eynzof/Hermes-CN-Desktop#610 ·
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
gitbutlerapp/gitbutler#15998 · 1 comment ·
-
bug triage:deciding
Difficulty 1/5 Under an hour Newbie friendliness 88/100
open-telemetry/otel-arrow#4132 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100