FAQ - Help and questions

Where should a desktop app store its settings, data and cache on Windows and macOS?

#settings#paths#windows#macos#dirspublished 0 replies0 views
ReplyWatch
Share
Hacker NewsRedditLinkedInE-mail

Short answer

Use the per-user folders the system provides, not the program's own folder. The dirs crate returns them: on Windows the AppData Roaming and Local folders, on macOS Library Application Support and Library Caches. Put a folder named after your app inside, and keep settings, data and cache apart.

Checked with rustc 1.93.0, Windows 11, dirs 6.0.0; compiled and run on Windows (output below is real, with the home folder shown as ~). The macOS paths come from the dirs documentation and were not run on a Mac.

Frequently asked question, written and answered by the Ruststudio team. Replies are open: corrections are welcome.

#1

Answer#

A program should not write next to its executable. On Windows an app installed under Program Files usually cannot write there without administrator rights, and on macOS an app bundle should stay unchanged (changing it can break its code signature). Every user also needs their own settings.

Both systems define per-user folders for this. The dirs crate asks the system for them, so you do not hard-code paths. The table is from the crate's documentation (read 2026-12-05); the Windows column was also run, see below.

dirs functionWindowsmacOSUse it for
config_dir()AppData\Roaming (the Known Folder RoamingAppData)~/Library/Application Supportsettings the user would want on another PC (on Windows this folder can roam with a domain profile)
config_local_dir()AppData\Local~/Library/Application Supportsettings tied to this machine
data_dir() / data_local_dir()AppData\Roaming / AppData\Local~/Library/Application Supportdocuments, databases, downloaded content
cache_dir()AppData\Local~/Library/Cachesthings you can rebuild; the user or the system may delete them

In every case you add your own subfolder: config_dir().join("MyApp"). Never write directly into the shared folder.

This program prints each folder relative to the home folder, so the output does not contain a user name:

src/main.rsrust
fn main() {
    let home = dirs::home_dir().expect("no home folder");
    // Print each folder relative to the home folder, so the output does not depend on the user name.
    let show = |name: &str, p: Option<std::path::PathBuf>| match p {
        Some(p) => match p.strip_prefix(&home) {
            Ok(rel) => println!("{name:<16} ~\\{}", rel.display()),
            Err(_) => println!("{name:<16} (outside the home folder)"),
        },
        None => println!("{name:<16} none"),
    };
    show("config_dir", dirs::config_dir());
    show("config_local_dir", dirs::config_local_dir());
    show("data_dir", dirs::data_dir());
    show("data_local_dir", dirs::data_local_dir());
    show("cache_dir", dirs::cache_dir());
}
text
config_dir       ~\AppData\Roaming
config_local_dir ~\AppData\Local
data_dir         ~\AppData\Roaming
data_local_dir   ~\AppData\Local
cache_dir        ~\AppData\Local

(The ~\ is the program's own formatting; the real folders start with the full path of the user's home folder.)

Each function returns an Option: handle None (for example by running without saved settings) instead of unwrap.

Common mistakes#

  • Writing to the current directory. It is whatever folder the program was started from (see the thread on the executable's folder).
  • Putting a cache in the settings folder. Backups and roaming profiles would then carry data that is only a cache.
  • Storing passwords or tokens in a plain settings file. Use the system's credential store through a crate for it, or ask for the secret again.

Does a dirs path include my app's name?#

No. It is the shared folder for all apps; you add the subfolder.

What about Linux?#

dirs follows the XDG folders there (~/.config, ~/.local/share, ~/.cache) according to its documentation. Not run here.

Sources#

    QuoteReply
    Share
    Hacker NewsRedditLinkedInE-mail

    No replies yet