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