6

I need to persist information, such as the user's configuration for my program and their input. Where would the best place be to save this files on Linux and Windows?

This is for a rust-cargo program, which will be installed into ~/.cargo/bin for its usage. I have tried to save my files into ~/.cargo but I don't know if this is appropiate, as I am new into Rust.

Mario Codes
  • 689
  • 8
  • 15
  • 2
    There are several libraries providing XDG paths like the ones normally relevant for user configuration, for example https://github.com/soc/directories-rs. Is this what you're looking for ? – Denys Séguret Oct 24 '19 at 12:23
  • If there is no standard place in Rust I would use it, but I would prefer to manage them myself and not depend on another library for it. – Mario Codes Oct 24 '19 at 12:43
  • 1
    There's no reason for another standard just because the application is written in Rust. If you don't want to use a lib, then you just have to read the `$XDG_CONFIG_HOME` env var. The libraries are mostly useful for fallbacks, windows compatibility, etc. – Denys Séguret Oct 24 '19 at 12:45
  • Possible duplicate of [Location of ini/config files in linux/unix?](https://stackoverflow.com/questions/1024114/location-of-ini-config-files-in-linux-unix) – trent Oct 24 '19 at 13:22
  • I added the linux tag as the question is obviously related to this OS. OP feel free to revert if you were looking for an OS independent answer. – Denys Séguret Oct 24 '19 at 13:44
  • Added also Windows tag as my question was directed towards Linux, but I'd be also interested on the answer for Windows for future reference. – Mario Codes Oct 24 '19 at 13:48
  • If you develop a cross-platform application, you should in all cases consider using libraries like [dirs](https://crates.io/crates/dirs) that platform-independently provide standard folder paths – Sty Oct 24 '19 at 13:51
  • See also: [Location to put user configuration files in windows](https://stackoverflow.com/q/2243895/3650362), [Program configuration data in Unix/Linux](https://stackoverflow.com/q/2384933/3650362), [Registry vs. INI file for storing user configurable application settings](https://stackoverflow.com/q/6607/3650362), [How to decide where to store per-user state? Registry? AppData? Isolated Storage?](https://stackoverflow.com/q/882490/3650362) As other commenters have observed, choice of language is orthogonal to where config files should go. – trent Oct 24 '19 at 14:41

1 Answers1

10

There's nothing special for applications written in Rust. Contrary to other solutions coming with a runtime, Rust builds normal applications relying on the standard env and practices. The normal rules of the system apply for configuration locations.

On linux you should first query the XDG paths then use $HOME as fallback when it's not available.

Here's how you can do it:

use std::env::var;

fn main() {
    let config_home = var("XDG_CONFIG_HOME")
        .or_else(|_| var("HOME").map(|home|format!("{}/.config", home)));
    println!("{:?}", config_home);
}

Note that several libraries do this job for you and take care of supporting alternate operating systems.

I won't link to them because they are many of them and they often change but your favourite search engine will direct you to the most popular ones if you search for "rust configuration directory".

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • I'd add that this applies even to cargo extensions or cargo-distributed binaries (which are the only ones I would expect to be placed in `~./cargo/bin/`). One can compare to how e.g. [ripgrep](https://github.com/BurntSushi/ripgrep/blob/master/GUIDE.md#configuration-file) or [rustfmt](https://github.com/rust-lang/rustfmt/blob/master/Configurations.md) approach this – Sty Oct 24 '19 at 13:48