Below is a a code snippet where I am trying to set a static globally available String value using lazy_static. The code below runs, but it seems clunky to me, that whenever I want to get the value I have to call:
(*CONFIG_PATH).to_string()
in order to avoid the error:
error[E0507]: cannot move out of dereference of `CONFIG_PATH`
...
| ^^^^^^^^^^^^ move occurs because value has type `std::string::String`, which does not implement the `Copy` trait
Is there some other way to do this more elegantly? Minimal code example and link to rust playground is below.
use std::{fs, env};
use lazy_static::lazy_static;
pub const DEFAULT_CONFIG_PATH: &str = "/var/lib/peachcloud/config.yml";
lazy_static! {
static ref CONFIG_PATH: String = {
if let Ok(val) = env::var("CONFIG_PATH") {
val
}
else {
DEFAULT_CONFIG_PATH.to_string()
}
};
}
fn main() {
println!("CONFIG_PATH: {}", *CONFIG_PATH);
fs::write((*CONFIG_PATH).to_string(), "test output");
}