|
|
|
@ -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`。
|
|
|
|
|