arg does not live long enough borrowed value does not live long enough main.rs(59, 5): arg dropped here while still borrowed main.rs(56, 13): binding arg declared here main.rs(42, 9): borrow later stored here
im so confused
let args = if params.feature.any_feature() {
let mut features = Vec::new();
if params.feature.feature0 {
features.push("feature0")
}
if params.feature.feature1 {
features.push("feature1")
}
if params.feature.feature2 {
features.push("feature2")
}
if params.feature.feature3 {
features.push("feature3")
}
let arg = features.join(",");
vec!["build", "--release", "--features", &arg]
} else {
vec!["build", "--release"]
};
You can't store a reference to a variable for longer than the variable exists:
let args = if true {
let mut features = Vec::<&str>::new();
let arg = features.join(",");
vec!["build", "--release", "--features", &arg]
// Here at the end of the block, `arg` goes out of scope
// and gets destructed (the `String` gets deallocated).
// That's not compatible with `&arg` being in `args`, which
// lasts beyond this block.
} else {
vec!["build", "--release"]
};
There's ways to make this work with args: Vec<&str> within a single function body, but your best bet is probably going to be to use a Vec<String> instead of a Vec<&str>. Then the args will own the data and you don't have to worry about the lifetime problems.
let args = if true {
let mut features = Vec::<&str>::new();
let arg = features.join(",");
vec![
"build".to_string(),
"--release".to_string(),
"--features".to_string(),
arg,
]
} else {
vec!["build".to_string(), "--release".to_string()]
};