[Solved] Not live long enough with CSV and dataflow


The problem is that you are pushing to the Vec a borrowed value: &record[i]. The & means borrow, and as a consequence the original value record must outlive the borrower vec.

That might seem fine (both are in the for body, and thus both have the same lifetime, i.e., they both live inside the for body and therefore none outlive each other), but this doesn’t happen because the line input.insert(vec) is moving vec. What this means is that vec now becomes owned by input and hence it lives as long as input (as far as I understand). Now, because input is outside the for body, the moved vec lives as long as input and therefore outlives the record[i]s.

There are a few solutions, but all of them try to remove the dependency between record and input:

  1. If the record is an array of primitive values, or something that implements the Copy trait, you can simply omit the borrow and the value will be copied into the vector: vec.push(record[i]).
  2. Clone the record value into the vector: vec.push(record[i].clone()). This forces the creation of a clone, which as above, the vec becomes the owner, avoiding the borrow.
  3. If the elements in the record array don’t implement Copy nor Clone, you have to move it. Because the value is in an array, you have to move the array fully (it can’t have elements that haven’t been removed). One solution is to transform it into an iterator that moves out the values one by one, and then push them into the vector:

    for element in record.into_iter().take(13) {
        vec.push(element)
    }
    
  4. Replace the record value with a different value. One final solution in order to move only parts of the array is to replace the element in the array with something else. This means that although you remove an element from the array, you replace it with something else, and the array continues to be valid.

    for i in 0..13 {
        vec.push(std::mem::replace(&record[i], Default::default()));
    }
    

    You can replace Default::default() with another value if you want to.

I hope this helps. I’m still a noob in Rust, so improvements and critique on the answer are accepted 🙂

5

solved Not live long enough with CSV and dataflow