mirror of
https://github.com/KaiserY/trpl-zh-cn
synced 2024-11-09 08:51:18 +08:00
Update ch10-03-lifetime-syntax.md
fix output message
This commit is contained in:
parent
538bcf748a
commit
ad55920256
@ -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`。
|
||||||
|
Loading…
Reference in New Issue
Block a user