Cargo.toml
threadpool = "1.8.1"
I am using threadpool crate, just for learning purposes, if there are some better threadpool crates please inform me.
Code with problem, simplified version:
use threadpool::ThreadPool;
fn main() {
let pool = ThreadPool::new(4);
let v2 = vec!["a".to_string(), "b".to_string(), "c".to_string(), ];
for i in v2.iter() { // .iter() is the problem
pool.execute(move || println!("{}", i));
}
pool.join();
}
Error:
error[E0597]: `v2` does not live long enough
for i in v2.iter() { // .iter() is the problem
| ^^^^^^^^^ borrowed value does not live long enough
pool.execute(move || println!("{}", i));
| --------------------------------------- argument requires that `v2` is borrowed for `'static`
It is working fine without .iter(). Reason why I use it, it is for demonstration purposes in my code I use iter and zip to iter thru 2 vec of same size both of type String eg. urls.iter().zip(files_t4.iter())
.
I do understand why I have error, but have no ideas how to solve it ?
I know that this is similar to How can I pass a reference to a stack variable to a thread?, but except trying another crate, not sure is it possible to use std::thread::scope
like in first example.
I am learning hot to download files in Rust, and now what to see dow it is done with threadpool.
Any ideas are appreciated.