casibbald
casibbald

Reputation: 3135

Rust PathBuf issues when building relative dir from home

static CONFIG_FILE: &str = "/.config/myapp/config.toml";

but I want to build an os agnostic path:

$HOME/.config/myapp/config.toml

Tried variations of:

static CONFIG_FILE: &str = dirs::home_dir().unwrap().join(".config").join("myapp").join("config.toml").to_str().unwrap();

but I get:

   |
28 | static  CONFIG_FILE: &str = dirs::home_dir().unwrap().join(".config").join("myapp").join("config.toml").to_str().unwrap();
   |                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `~const Deref` is not implemented for `PathBuf`

The following seems to compile however the end part of the path is not platform agnostic.

/// Application configuration filename
static HOME: Lazy<String> = Lazy::new(|| { env::var("HOME").unwrap() } );
static CONFIG_FILE: Lazy<PathBuf> = Lazy::new(||{Path::new(&HOME.as_str()).join(".config/assimilate/config.toml")});

Upvotes: 3

Views: 379

Answers (1)

Solomon Ucko
Solomon Ucko

Reputation: 6109

Try this:

static CONFIG_FILE: Lazy<PathBuf> = Lazy::new(||{dirs::home_dir().unwrap().join(".config").join("myapp").join("config.toml")});

Upvotes: 2

Related Questions