In this code everything works except task_id
. I want this script to count requests in task_id
:
use std::thread;
use std::thread::sleep_ms;
use std::sync::mpsc;
#[macro_use] extern crate nickel;
use nickel::Nickel;
fn main() {
let mut server = Nickel::new();
let mut task_id: i64 = 0;
server.utilize(router! {
get "**" => |_req, _res| {
task_id += 1;
run_heavy_task(task_id);
"Yo!"
}
});
server.listen("127.0.0.1:6767");
}
fn run_heavy_task(task_id: i64) {
let (tx, rx) = mpsc::channel();
thread::spawn(move || {
println!("heavy task {} started!", task_id);
sleep_ms(3000);
println!("heavy task {} completed", task_id);
let result = tx.send(());
});
//rx.recv();
//println!("Task {} completed", task_id);
}
error:
can't capture dynamic environment in a fn item; use the || { ... } closure form instead main.rs:13 task_id += 1;
Please help me solve this issue - how can I pass task_id into closure?