Update ch10-03-lifetime-syntax.md

fix output message
This commit is contained in:
Orefa 2020-09-10 07:10:17 +08:00 committed by GitHub
parent 538bcf748a
commit ad55920256
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
--> src/main.rs:6:44
|
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
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`