I am using clap.rs to parse command line arguments and I am struggling with lifetimes and borrowing with String
s:
fn get_build_tool_kind_argument<'a>(short_arg: &'a str, long_arg : &'a str) -> Arg<'a, 'a> {
let argument_name = format!("{}_pouet", long_arg);
Arg::with_name(&argument_name)
.short(short_arg)
.long(long_arg)
.takes_value(false)
.help("Configure elements for maven build tool")
}
To call the function, I do get_build_tool_kind_argument("m", "maven")
.
Here is the compilation error I get:
error[E0597]: `argument_name` does not live long enough
--> src\main.rs:29:21
|
29 | Arg::with_name(&argument_name)
| ^^^^^^^^^^^^^ borrowed value does not live long enough
...
34 | }
I have seen a lot of questions and documentation but none seem to apply in my case. I still have limited comprehension about lifetimes.
From what I see here, the borrowed formatted value I inject to my Arg
name is freed at the end of the function, so it can't outlive it and be used when I call get_build_tool_kind_argument
. I can't find a way to keep using the format!
macro and solve my problem.