I was recently writing a script, and I got the following error:
error[E0597]: borrowed value does not live long enough
Here is my code:
fn main(){
let mut arr = ["Hello", " ", "world!"];
let mut res = "";
for i in arr.iter() {
res = format!("{}{}", res, i).as_str();
}
}
I cannot figure out why this is erring, here is my full error:
error[E0597]: borrowed value does not live long enough
--> test.rs:5:48
|
5 | res = format!("{}{}", &res, i).as_str();
| ------------------------ ^ temporary value dropped here while still borrowed
| |
| temporary value created here
6 | }
7 | }
| - temporary value needs to live until here
|
= note: consider using a `let` binding to increase its lifetime
= note: this error originates in a macro outside of the current crate
Could someone please explain this?