user1653651
user1653651

Reputation: 21

Resolving borrowed value issue

I have the following simple code. I am not able to figure out how to resolve the error "borrowed value does not live long enough".

fn main() {
    let mut saved_message: Vec<&str> = Vec::new();
    for i in 0..10 {
        let mut tmp: String = String::from("line_num ");
        tmp.push_str(&i.to_string());
        saved_message.push(&tmp);
    }
}
   Compiling rust_test v0.1.0 (C:\workspace\rust_test)
error[E0597]: `tmp` does not live long enough
 --> src\lib.rs:6:28
  |
6 |         saved_message.push(&tmp);
  |         -------------------^^^^-
  |         |                  |
  |         |                  borrowed value does not live long enough
  |         borrow later used here
7 |     }
  |     - `tmp` dropped here while still borrowed

For more information about this error, try `rustc --explain E0597`.
error: could not compile `rust_test` due to previous error

Upvotes: 1

Views: 182

Answers (1)

Arshia Akhavan
Arshia Akhavan

Reputation: 89

Error comes from this line:

    let mut saved_message: Vec<&str> = Vec::new();

Here, you defined a vector of references to string slices.

Since you are storing references instead of the whole string, every reference that is stored in the vector, should outlive the vector itself. But this is not the case with your code as each tmp value in your for loop gets droped at the end of the loop (reference is no longer valid) and thus, pushing it to a vector that would outlive the for loop is not safe.

workarounds: Create a vector of String instead of &str

fn main() {
    let mut saved_message: Vec<String> = Vec::new();
    for i in 0..10 {
        let mut tmp: String = String::from("line_num ");
        tmp.push_str(&i.to_string());
        saved_message.push(tmp);
    }
}

Notice the vector type hint which is now Vec<String> instead of Vec<&str>. Note that now you should push tmp instead of &tmp in the last line. You can also trust the compiler to guess the type for you and remove the whole type hint:

fn main() {
    let mut saved_message = Vec::new();
    for i in 0..10 {
        let mut tmp = String::from("line_num ");
        tmp.push_str(&i.to_string());
        saved_message.push(tmp);
    }
}

Upvotes: 3

Related Questions