Merge pull request #471 from Orefa/patch-3

Update ch10-03-lifetime-syntax.md
This commit is contained in:
KaiserY 2020-09-10 10:22:42 +08:00 committed by GitHub
commit 7d75869d30
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -233,15 +233,14 @@ fn main() {
```text
error[E0597]: `string2` does not live long enough
--> src/main.rs:15:5
|
14 | result = longest(string1.as_str(), string2.as_str());
| ------- borrow occurs here
15 | }
| ^ `string2` dropped here while still borrowed
16 | println!("The longest string is {}", result);
17 | }
| - borrowed value needs to live until here
--> src/main.rs:6:44
|
6 | result = longest(string1.as_str(), string2.as_str());
| ^^^^^^^ borrowed value does not live long enough
7 | }
| - `string2` dropped here while still borrowed
8 | println!("The longest string is {}", result);
| ------ borrow later used here
```
错误表明为了保证 `println!` 中的 `result` 是有效的,`string2` 需要直到外部作用域结束都是有效的。Rust 知道这些是因为(`longest`)函数的参数和返回值都使用了相同的生命周期参数 `'a`