我有一些 url,然后我将其分成块并同时迭代它们,使用克隆将结果保存在 vec 中:
pub fn collect_parm_list(url_list: Vec<String>) -> Result<Vec<String>, Box<dyn std::error::Error>> {
let shared_id_list = Arc::new(Mutex::new(vec![]));
for chunk in url_list.chunks(20) {
let mut handles = vec![];
for url in chunk {
let id_list = Arc::clone(&shared_id_list);
let handle = thread::spawn(move || {
// total: optionSale -> result -> total
let mut id_list_v = id_list.lock().unwrap();
id_list_v.push(url.to_string())
});
handles.push(handle);
}
// Wait for all threads to complete
for handle in handles {
handle.join().unwrap();
}
}
let x = shared_id_list.lock().unwrap();
Ok(x.to_vec())
}
我在借用数据时遇到错误:
url_list
does not live long enough borrowed value does not live long enough
在:对于url_list中的块。chunks(20){