Hi, I'm new to Rust so I'm sorry for the stupid question. I'm trying to do what this chapter is demonstrating how not to do.
Code:
// Start discovery routine
pub fn dsc(thread_pool: &ThreadPool, state: &mut State, neighbour_list: &Vec<String>) {
let node_list: Arc<Mutex<HashMap<String, Node>>> =
Arc::new(Mutex::new(HashMap::new()));
for host in neighbour_list {
let nodes = Arc::clone(&node_list);
thread_pool.execute(move || handshake(&host, nodes));
}
thread_pool.join();
state.change_mode(Mode::WRK);
}
fn handshake(host: &String, node_list: Arc<Mutex<HashMap<String, Node>>>) {
// do requests
}
Compile error:
error[E0621]: explicit lifetime required in the type of `neighbour_list`
--> src/discovery.rs:16:21
|
10 | pub fn dsc<'a>(thread_pool: &ThreadPool, state: &mut State, neighbour_list: &Vec<String>) {
| ------------ help: add explicit lifetime `'static` to the type of `neighbour_list`: `&'static std::vec::Vec<std::string::String>`
...
16 | thread_pool.execute(move || handshake(&host, nodes));
| ^^^^^^^ lifetime `'static` required
I do understand it's not compiling because there are no lifetime guarantees for the contents of neighbour_list
after the execute call. The calling thread could just exit at any time. However, I intend to wait for each job to finish with the thread_pool.join()
call. I know about lifetimes, but I don't know how I can infer the them here. Or is this not the right approach at all?