Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Value does not live long enough when using the Chunks iterator across multiple threads

Tags:

rust

This is a simplified example of my situation:

use std::sync::mpsc::{Sender, Receiver};
use std::sync::mpsc;
use std::thread;

struct User {
    reference: String,
    email: String,
}

fn main() {
    let rows = vec![
        vec!["abcd", "[email protected]"],
        vec!["efgh", "[email protected]"],
        vec!["wfee", "[email protected]"],
        vec!["rrgr", "[email protected]"],
    ];

    let chunk_len = (rows.len() / 2) as usize;
    let mut chunks = Vec::new();
    for chunk in rows.chunks(chunk_len) {
        chunks.push(chunk);
    }

    let (tx, rx): (Sender<Vec<User>>, Receiver<Vec<User>>) = mpsc::channel();

    for chunk in chunks {
        let thread_tx = tx.clone();
        thread::spawn(move || {
            let result = chunk
                .iter()
                .map(|row| {
                    User {
                        reference: row[0].to_string(),
                        email: row[1].to_string(),
                    }
                })
                .collect::<Vec<User>>();
            thread_tx.send(result).unwrap();
        });
    }

    let mut users = Vec::new();
    for _ in 0..chunk_len {
        users.push(rx.recv());
    }
}

and it's throwing an error

error[E0597]: `rows` does not live long enough
  --> src/main.rs:20:18
   |
20 |     for chunk in rows.chunks(chunk_len) {
   |                  ^^^^ does not live long enough
...
47 | }
   | - borrowed value only lives until here
   |
   = note: borrowed value must be valid for the static lifetime...

I tried to change to chunks.push(chunk.clone()); but the error still wouldn't go away. What am I missing here?

like image 316
Caballero Avatar asked Feb 10 '23 20:02

Caballero


1 Answers

The error does look misleading, however, it is correct. Your problem in fact is that chunks() gives an iterator of slices into the original vector:

impl<'a, T> Iterator for Chunks<'a, T> {
    type Item = &'a [T];
}

You're trying to use this slice in a spawn()ed thread which requires the closure to have the 'static lifetime:

pub fn spawn<F, T>(f: F) -> JoinHandle<T> 
where
    F: FnOnce() -> T,
    F: Send + 'static,
    T: Send + 'static, 

Your iterator does have the 'static lifetime because it contains references to a runtime-allocated vector.

You said that you tried clone(), but that only clones the slice, it does not give you a new vector. For that you need to use to_owned():

for chunk in rows.chunks(chunk_len) {
    chunks.push(chunk.to_owned());
}

And lo, it compiles.

like image 153
Vladimir Matveev Avatar answered Feb 12 '23 10:02

Vladimir Matveev