I am trying to set a variable based on CLI option and then move it to async closure. But borrow-checker says the variable does not live long enough, seems due to async closure. Here is the code:
#[tokio::main]
async fn main() {
let matches = clap::App::new("MyApp")
.version("0.1.0")
.arg(
Arg::with_name("root")
.short("r")
.long("root")
.takes_value(true)
.help("Root directory"),
)
.get_matches();
let root_dir = matches.value_of("root").unwrap_or("/home");
// A `Service` is needed for every connection
let make_svc = make_service_fn(|_conn| async move {
Ok::<_, Error>(service_fn(move |req| async move {
hello_world(req, root_dir).await
}))
});
Note that root_dir type is &static str, but because it's moved into async closure, borrow-checker deems it not living long enough. Any way to get around it? (I am trying to avoid create String` and change hello_world , unless that's the only way.)
And note that if I hard-code root_dir (not get it from CLI), it will work:
let root_dir = "/home";
Even though the type is the same (&'static str) . Why?