Search code examples
loopsrustvectorscope

Pushing values constructed in a loop onto a vector outside the loop


I'm just getting my toes wet with Rust and I hit a problem which I do understand, but am unable to fix.

Inside a loop I construct some data. I want to push this data onto a vector defined outside the loop. However, the data has an underlying data structure that goes out-of-scope at the end of the loop.

In the given example, how would I "copy" the referenced item to the vector outside the loop? Or can you explain why what i'm trying to do is frowned upon.

use std::io::{self, BufRead};

/*
 * Let's read some lines from stdin and split them on the delimiter "|".
 * Store the string on the left side of the delimiter in the vector
 * "lines_left".
 */

fn main() {
    let stdin = io::stdin();

    // in "scope: depth 1" we have a mutable vector called "lines_left" which
    // can contain references to strings.
    let mut lines_left: Vec<&str> = Vec::new();

    // For each line received on stdin, execute the following in
    // "scope: depth 2".
    for stdin_result in stdin.lock().lines() {
        // _Jedi handwave_ "there is no error". Store the "str" in the inmutable
        // variable "string". (Type = "&str" ).
        let string = stdin_result.unwrap();

        // Split the string on the "|" character. This results in a vector
        // containing references to strings. We call this vector "words".
        let words: Vec<&str> = string.split("|").collect();

        // Push the string reference at index 0 in the vector "lines_left".
        // FIXME: This is not allowed because:
        // The string reference in the vector "words" at index 0,
        // points to the underlying data structure "string" which is defined
        // in "scope: depth 2" and the data structure "lines_left" is defined
        // in "scope: depth 1". "scope: depth 2" will go out-of-scope below
        // this line. I need a "copy" somehow...
        lines_left.push(words[0])
    }
}

Compiling this code leads to:

error[E0597]: `string` does not live long enough
  --> main.rs:25:32
   |
21 |         let string = stdin_result.unwrap();
   |             ------ binding `string` declared here
...
25 |         let words: Vec<&str> = string.split("|").collect();
   |                                ^^^^^^^^^^^^^^^^^ borrowed value does not live long enough
...
34 |         lines_left.push(words[0])
   |         ------------------------- borrow later used here
35 |     }
   |     - `string` dropped here while still borrowed

error: aborting due to previous error

For more information about this error, try `rustc --explain E0597`.

So guess I could make a copy of the thing referenced with words[0]?

Thank you for your time.


Solution

  • You're on the right track, you need a copy indeed. But the type of lines_left is wrong, it can't hold references because references must refer to something longer-lived. Therefore:

    In the given example, how would I "copy" the referenced item to the vector outside the loop?

    Change the type of lines_left from Vec<&str> to Vec<String>, and append with lines_left.push(words[0].to_string()). to_string() conveniently copies a &str into a freshly created owned String.

    Playground