mirror of
https://github.com/KaiserY/trpl-zh-cn
synced 2024-11-09 08:51:18 +08:00
Update ch19-02-advanced-lifetimes.md
This commit is contained in:
parent
8a88048582
commit
6f8df93589
@ -93,25 +93,17 @@ body at 15:55...
|
|||||||
| |_^
|
| |_^
|
||||||
```
|
```
|
||||||
|
|
||||||
These errors are saying that both the `Parser` instance we're creating and the
|
这些错误表明不管是我们创建的`Parser`实例还是作用于从`Parser`被创建的行开始到`parse_context`函数结束的`context`参数, 都需要拥有整个函数的生命周期.
|
||||||
`context` parameter live from the line that the `Parser` is created until the
|
|
||||||
end of the `parse_context` function, but they both need to live for the entire
|
|
||||||
lifetime of the function.
|
|
||||||
|
|
||||||
In other words, `Parser` and `context` need to *outlive* the entire function
|
换句话说, `Parser`和`context`存活的时间要比整个函数更长, 为了让代码中的所有引用都有效它们也应该在函数被调用前后都有效. 不管是我们正创建的`Parser`还是在函数结束时就会结束作用域的`context`参数都是如此(因为`parse_context`函数会获得`context`的所有权).
|
||||||
and be valid before the function starts as well as after it ends in order for
|
|
||||||
all the references in this code to always be valid. Both the `Parser` we're
|
|
||||||
creating and the `context` parameter go out of scope at the end of the
|
|
||||||
function, though (since `parse_context` takes ownership of `context`).
|
|
||||||
|
|
||||||
Let's look at the definitions in Listing 19-13 again, especially the signature
|
让我们再看一下例19-13中的定义, 特别是`parse`方法的声明:
|
||||||
of the `parse` method:
|
|
||||||
|
|
||||||
```rust,ignore
|
```rust,ignore
|
||||||
fn parse(&self) -> Result<(), &str> {
|
fn parse(&self) -> Result<(), &str> {
|
||||||
```
|
```
|
||||||
|
|
||||||
Remember the elision rules? If we annotate the lifetimes of the references, the
|
还记得生命周期的省略规则吗? If we annotate the lifetimes of the references, the
|
||||||
signature would be:
|
signature would be:
|
||||||
|
|
||||||
```rust,ignore
|
```rust,ignore
|
||||||
|
Loading…
Reference in New Issue
Block a user