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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With