I am trying to add a webserver (hyper
) in a small Rust program and I'm getting hit with a move issue.
//main.rs
// Suppose this is something meaningful and used in multiple places inside `main`
let test: String = "Foo".to_string();
// ...
// This comes from here: https://docs.rs/hyper/0.14.8/hyper/service/fn.service_fn.html
// Determines what the web server sends back
let make_svc = make_service_fn(|_conn| async {
Ok::<_, Infallible>(service_fn(|req: Request<Body>| async move {
// I need to somehow read the value of `test` here as well
if req.version() == Version::HTTP_11 {
Ok(Response::new(Body::from(test)))
} else {
Err("not HTTP/1.1, abort connection")
}
}))
});
This produces the following issue:
I understand that String can't have the Copy trait (and eventually I will need to use other more complex types). Essentially, I want to borrow or use a clone of the variable, because the variables will also be used outside of hyper's handler (for example, it could log to a file or be used for aggregate statistics over time).
So my question is, how does it work? How do I need to refactor the closure like so that I can somehow access the values of variables defined (and otherwise used) inside main?