I wrote a simple Rust program. Depending on the order of my variable declaration, compilation succeeds or fails.
fn main() {
let my_env: &String;
let options: Vec<String> = ["a", "b", "c"].iter().map(|s| s.to_string()).collect();
my_env = &options[1];
let _ = my_env;
}
error[E0597]: `options` does not live long enough
--> src/main.rs:4:15
|
4 | my_env = &options[1];
| ^^^^^^^ borrowed value does not live long enough
5 | let _ = my_env;
6 | }
| - `options` dropped here while still borrowed
|
= note: values in a scope are dropped in the opposite order they are created
When I declare my_env
right after the options
, the compilation is a success. Is this a Rust feature or a bug?