Why is this an issue? What is the ownership problem?
fn test_code() -> String {
let mut mod_string = "";
let y = ["", "", " "];
for x in &y {
if x.to_string() == " " {
// getting temp value dropped below... why?
mod_string = &(mod_string.to_owned() + &x.to_string() + ",");
}
}
mod_string.to_string()
}
error[E0716]: temporary value dropped while borrowed
--> src/lib.rs:9:27
|
9 | mod_string = &(mod_string.to_owned() + &x.to_string() + ",");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- temporary value is freed at the end of this statement
| |
| creates a temporary which is freed while still in use
...
13 | mod_string.to_string()
| ---------- borrow later used here
|
= note: consider using a `let` binding to create a longer lived value