Home

Awesome

crates.io API documentation actively developed License: MIT/Apache-2.0

dirs

Introduction

The library provides the location of these directories by leveraging the mechanisms defined by

Platforms

This library is written in Rust, and supports Linux, Redox, macOS and Windows. Other platforms are also supported; they use the Linux conventions.

The minimal required version of Rust is 1.13 except for Redox, where the minimum Rust version depends on the redox_users crate.

It's mid-level sister library, directories, is available for Rust (directories-rs) and on the JVM (directories-jvm).

Usage

Dependency

Add the library as a dependency to your project by inserting

dirs = "5.0"

into the [dependencies] section of your Cargo.toml file.

If you are upgrading from version 2, please read the section on breaking changes first.

Example

Library run by user Alice:

extern crate dirs;

dirs::home_dir();
// Lin: Some(/home/alice)
// Win: Some(C:\Users\Alice)
// Mac: Some(/Users/Alice)

dirs::audio_dir();
// Lin: Some(/home/alice/Music)
// Win: Some(C:\Users\Alice\Music)
// Mac: Some(/Users/Alice/Music)

dirs::config_dir();
// Lin: Some(/home/alice/.config)
// Win: Some(C:\Users\Alice\AppData\Roaming)
// Mac: Some(/Users/Alice/Library/Application Support)

dirs::executable_dir();
// Lin: Some(/home/alice/.local/bin)
// Win: None
// Mac: None

Design Goals

Features

If you want to compute the location of cache, config or data directories for your own application or project, use ProjectDirs of the directories project instead.

Function nameValue on Linux/RedoxValue on WindowsValue on macOS
home_dirSome($HOME)Some({FOLDERID_Profile})Some($HOME)
cache_dirSome($XDG_CACHE_HOME) or Some($HOME/.cache)Some({FOLDERID_LocalAppData})Some($HOME/Library/Caches)
config_dirSome($XDG_CONFIG_HOME) or Some($HOME/.config)Some({FOLDERID_RoamingAppData})Some($HOME/Library/Application Support)
config_local_dirSome($XDG_CONFIG_HOME) or Some($HOME/.config)Some({FOLDERID_LocalAppData})Some($HOME/Library/Application Support)
data_dirSome($XDG_DATA_HOME) or Some($HOME/.local/share)Some({FOLDERID_RoamingAppData})Some($HOME/Library/Application Support)
data_local_dirSome($XDG_DATA_HOME) or Some($HOME/.local/share)Some({FOLDERID_LocalAppData})Some($HOME/Library/Application Support)
executable_dirSome($XDG_BIN_HOME) or Some($HOME/.local/bin)NoneNone
preference_dirSome($XDG_CONFIG_HOME) or Some($HOME/.config)Some({FOLDERID_RoamingAppData})Some($HOME/Library/Preferences)
runtime_dirSome($XDG_RUNTIME_DIR) or NoneNoneNone
state_dirSome($XDG_STATE_HOME) or Some($HOME/.local/state)NoneNone
audio_dirSome(XDG_MUSIC_DIR) or NoneSome({FOLDERID_Music})Some($HOME/Music/)
desktop_dirSome(XDG_DESKTOP_DIR) or NoneSome({FOLDERID_Desktop})Some($HOME/Desktop/)
document_dirSome(XDG_DOCUMENTS_DIR) or NoneSome({FOLDERID_Documents})Some($HOME/Documents/)
download_dirSome(XDG_DOWNLOAD_DIR) or NoneSome({FOLDERID_Downloads})Some($HOME/Downloads/)
font_dirSome($XDG_DATA_HOME/fonts/) or Some($HOME/.local/share/fonts/)NoneSome($HOME/Library/Fonts/)
picture_dirSome(XDG_PICTURES_DIR) or NoneSome({FOLDERID_Pictures})Some($HOME/Pictures/)
public_dirSome(XDG_PUBLICSHARE_DIR) or NoneSome({FOLDERID_Public})Some($HOME/Public/)
template_dirSome(XDG_TEMPLATES_DIR) or NoneSome({FOLDERID_Templates})None
video_dirSome(XDG_VIDEOS_DIR) or NoneSome({FOLDERID_Videos})Some($HOME/Movies/)

Comparison

There are other crates in the Rust ecosystem that try similar or related things. Here is an overview of them, combined with ratings on properties that guided the design of this crate.

Please take this table with a grain of salt: a different crate might very well be more suitable for your specific use case. (Of course my crate achieves my design goals better than other crates, which might have had different design goals.)

LibraryStatusLinMacWinBaseUserProjConv
app_dirsUnmaintainedāœ”āœ”āœ”šŸžˆāœ–āœ”āœ–
app_dirs2Maintainedāœ”āœ”āœ”šŸžˆāœ–āœ”āœ–
dirsDevelopedāœ”āœ”āœ”āœ”āœ”āœ–āœ”
directoriesDevelopedāœ”āœ”āœ”āœ”āœ”āœ”āœ”
s_app_dirUnmaintained?āœ”āœ–šŸžˆāœ–āœ–šŸžˆāœ–
standard_pathsMaintainedāœ”āœ–āœ”āœ”āœ”āœ”āœ–
xdgMaintainedāœ”āœ–āœ–āœ”āœ–āœ”šŸžˆ
xdg-basedirUnmaintained?āœ”āœ–āœ–āœ”āœ–āœ–šŸžˆ
xdg-rsObsoleteāœ”āœ–āœ–āœ”āœ–āœ–šŸžˆ

Build

It's possible to cross-compile this library if the necessary toolchains are installed with rustup. This is helpful to ensure a change hasn't broken code on a different platform.

The following commands will build this library on Linux, macOS and Windows:

cargo build --target=x86_64-unknown-linux-gnu
cargo build --target=x86_64-pc-windows-gnu
cargo build --target=x86_64-apple-darwin
cargo build --target=x86_64-unknown-redox

Changelog

5

4

3

2

BREAKING CHANGE The behavior of deactivated, missing or invalid XDG User Dirs entries on Linux has been improved (contributed by @tmiasko, thank you!):

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.