我原本以为这段代码可以正常工作,因为所有绑定都在同一个作用域中:
但我遇到了这个错误:
fn main() {
let mut foobar = vec!["foo"];
let bar = "bar".to_string();
foobar.push(&bar);
}
但我遇到了这个错误:
error: `bar` does not live long enough
--> baz.rs:4:18
|>
4 |> foobar.push(&bar);
|> ^^^
note: reference must be valid for the block suffix following statement 0 at 2:33...
--> baz.rs:2:34
|>
2 |> let mut foobar = vec!["foo"];
|> ^
note: ...but borrowed value is only valid for the block suffix following statement 1 at 3:32
--> baz.rs:3:33
|>
3 |> let bar = "bar".to_string();
|> ^
error: aborting due to previous error