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 function | Windows | macOS | Use it for |
|---|---|---|---|
config_dir() | AppData\Roaming (the Known Folder RoamingAppData) | ~/Library/Application Support | settings the user would want on another PC (on Windows this folder can roam with a domain profile) |
config_local_dir() | AppData\Local | ~/Library/Application Support | settings tied to this machine |
data_dir() / data_local_dir() | AppData\Roaming / AppData\Local | ~/Library/Application Support | documents, databases, downloaded content |
cache_dir() | AppData\Local | ~/Library/Caches | things 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:
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());
}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.
Related questions#
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#
- dirs crate documentation, platform table: https://docs.rs/dirs/6.0.0/dirs/ (read 2026-12-05)
- Microsoft, Known Folders: https://learn.microsoft.com/windows/win32/shell/known-folders (read 2026-12-05)
- Apple, File System Programming Guide, library directories: https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/MacOSXDirectories/MacOSXDirectories.html (read 2026-12-05)