My goal is to keep a static variable and have the value be overridden by CLI arguments but I'm having a hard time finding a way to keep a static copy of the value I get from the args iterator.
static mut ROOT_DIRECTORY: &str = "C:\\test\\source";
fn main() {
let args: Vec<String> = env::args().collect();
let mut index = 0;
for arg in args {
match arg.as_str() {
"/r" => unsafe {
if args_length <= index + 1 {
panic!("Missing root directory value.");
}
ROOT_DIRECTORY = args.get(index + 1).unwrap();
if ROOT_DIRECTORY.is_empty() {
panic!("Root directory value cannot be empty.")
}
}
}
}
}
Gives the following compilation error
error[E0597]: `args` does not live long enough
--> src\main.rs:90:34
|
90 | ROOT_DIRECTORY = args.get(index + 1).unwrap();
| ^^^^---------------
| |
| borrowed value does not live long enough
| argument requires that `args` is borrowed for `'static`
...
168 | }
| - `args` dropped here while still borrowed
error[E0382]: borrow of moved value: `args`
--> src\main.rs:90:34
|
54 | let args: Vec<String> = env::args().collect();
| ---- move occurs because `args` has type `std::vec::Vec<std::string::String>`, which does not implement the `Copy` trait
...
76 | for arg in args {
| ----
| |
| value moved here
| help: consider borrowing to avoid moving into the for loop: `&args`
...
90 | ROOT_DIRECTORY = args.get(index + 1).unwrap();
|
Is there any way for me to create a static copy of the value from the iterator?