---
title: How to Use the dirs Crate for Platform-Specific Directories
summary: Use the `dirs` crate to automatically retrieve platform-specific paths for configuration, data, and cache directories in Rust applications.
lang: en
slug: how-to-use-the-dirs-crate-for-platform-specific-directories
published: 2026-04-17T12:05:42+00:00
updated: 2026-04-25T22:00:13+00:00
canonical: https://www.rustfaq.org/en/how-to-use-the-dirs-crate-for-platform-specific-directories/
---

# How to Use the dirs Crate for Platform-Specific Directories



## You built a tool. Now where does it live?

You spent three weekends building a CLI tool that tracks your reading habits. It runs perfectly on your laptop. You zip it up and send it to a friend who uses Windows. They run the binary. Nothing happens. Or worse, it creates a folder called `.config` right next to the executable in their Downloads folder, cluttering their desktop. You forgot that "where to save stuff" is not a universal truth.

Operating systems have strong opinions about where applications should store configuration, data, and cache. Rust gives you the power to write to anywhere, but good software respects the platform's conventions. The `dirs` crate bridges that gap. It tells you exactly where the OS expects your files to live, so your app feels native everywhere.

## The universal translator for file paths

Every operating system organizes user files differently. Linux follows the XDG Base Directory Specification. macOS uses `~/Library/Application Support`. Windows scatters things across `AppData\Roaming`, `AppData\Local`, and `AppData\LocalLow`. If you hardcode a path, you're betting your app only runs on one system.

The `dirs` crate acts like a universal translator for file paths. You ask for "the config directory," and it returns the correct path for whatever OS is running your code. It handles the environment variables, registry keys, and fallback logic so you don't have to.

Think of it like moving into a new house. Different houses have different layouts. Some have a pantry, some have a closet, some have a mudroom. `dirs` is the realtor who shows you where the kitchen is, regardless of the house layout. You just ask for the kitchen, and you get the kitchen.

The functions return `Option<PathBuf>`. This is a signal. The OS might not have a standard location for what you're asking. You must handle the `None` case. Ignoring it leads to panics or silent failures.

Respect the OS. Your users will respect your app back.

## Minimal example

```rust
use dirs;

/// Prints the platform-specific config directory path.
fn main() {
    // Ask the crate for the config directory.
    // This returns Option<PathBuf>, not a direct path.
    if let Some(config_path) = dirs::config_dir() {
        // Unwrap is safe here because we matched Some.
        // Use display() for user-facing output.
        println!("Config lives at: {}", config_path.display());
    } else {
        // Handle the rare case where the OS has no standard config dir.
        eprintln!("Could not determine config directory.");
    }
}
```

Convention aside: Use `display()` for user-facing output. The `Debug` implementation of `PathBuf` prints quotes and escapes, which looks messy in logs. `display()` renders the path cleanly.

Always handle `None`. The path might not exist, and your code shouldn't crash just because the system is unique.

## What happens under the hood

When you call `dirs::config_dir()`, the crate checks the environment. On Linux, it looks for `XDG_CONFIG_HOME`. If that variable exists, it uses it. If not, it falls back to `~/.config`. On macOS, it constructs `~/Library/Application Support`. On Windows, it reads the registry or checks `APPDATA`.

The result is a `PathBuf`. This is an owned path string. You can pass it around, join components, or create files inside it. The `Option` wrapper protects you. Some systems might lack a user home directory or have a broken configuration. The crate returns `None` rather than guessing and potentially writing to a dangerous location.

If you need to pass the path to a function that takes `&Path`, use `.as_ref()` on the `Option`. This converts `Option<PathBuf>` to `Option<&Path>` without cloning the string data.

```rust
use dirs;

/// Demonstrates borrowing the path without cloning.
fn main() {
    // Convert Option<PathBuf> to Option<&Path>.
    // This avoids allocating a new PathBuf.
    if let Some(config_path) = dirs::config_dir().as_ref() {
        // Pass the reference to a helper function.
        print_path(config_path);
    }
}

/// Takes a borrowed path reference.
fn print_path(path: &std::path::Path) {
    println!("Path: {}", path.display());
}
```

If you try to pass a `PathBuf` where `&Path` is expected without borrowing, the compiler rejects you with `E0308` (mismatched types). The compiler expects a reference but finds an owned value. Add `&` or use `.as_path()` to fix it.

## Realistic example: Saving configuration

Most apps need to create a directory structure and write a file. The `dirs` crate gives you the base path. You handle the rest.

```rust
use dirs;
use std::fs;
use std::io::{self, Write};

/// Writes a default configuration file to the platform-specific directory.
fn save_default_config() -> io::Result<()> {
    // Get the config directory. Exit early if unavailable.
    // Map the None case to a meaningful error.
    let config_dir = dirs::config_dir().ok_or_else(|| {
        io::Error::new(io::ErrorKind::Other, "Config directory not found")
    })?;

    // Create a subdirectory for your app.
    // Always use a unique subdirectory to avoid collisions.
    let app_dir = config_dir.join("my-awesome-app");

    // Create the directory and all parents if they don't exist.
    // This is idempotent; it won't fail if the dir already exists.
    fs::create_dir_all(&app_dir)?;

    // Construct the full path to the config file.
    let config_file = app_dir.join("settings.json");

    // Write the default content.
    let mut file = fs::File::create(&config_file)?;
    file.write_all(b"{ \"theme\": \"dark\" }")?;

    Ok(())
}
```

Convention aside: Use `create_dir_all`. Never check if a directory exists before creating it. That creates a race condition where another process deletes the directory between the check and the creation. `create_dir_all` handles the existence check atomically.

Convention aside: Always create a subdirectory named after your app. Never write directly to the root of `config_dir`. If your app is named `my-tool`, create `my-tool` inside the config directory. This prevents collisions with other apps and keeps your files organized. In Cargo projects, you can access the package name via the `CARGO_PKG_NAME` environment variable at compile time using `env!("CARGO_PKG_NAME")`.

Create the directory structure first. Write the file second. Never assume the folder is already there.

## Config vs Data vs Cache

Understanding the difference between config, data, and cache prevents user frustration.

Configuration files are small and user-editable. Think `settings.json` or `config.toml`. Users expect to find these and modify them. Use `dirs::config_dir()` for these files.

Data directories hold larger, application-managed files. A local database or a collection of downloaded assets belongs here. Users generally shouldn't edit data files directly. Use `dirs::data_dir()` for these. On some systems, this returns the same path as config. On others, it returns a separate location. Your code should treat data as opaque to the user.

Cache directories contain discardable content. Pre-rendered templates, downloaded images, or temporary indexes live here. The OS may reclaim cache space when disk pressure is high. Use `dirs::cache_dir()` for these. Your app must handle missing cache files gracefully. If you store user data in the cache directory, you risk losing it when the system cleans up.

```rust
use dirs;
use std::fs;

/// Ensures the cache directory exists.
/// Returns false if the cache directory is unavailable.
fn ensure_cache_ready() -> bool {
    // Cache is optional. Return false if unavailable.
    let cache_dir = match dirs::cache_dir() {
        Some(path) => path,
        None => return false,
    };

    let app_cache = cache_dir.join("my-tool").join("assets");

    // Attempt to create the cache structure.
    // Log errors but don't fail. Cache is best-effort.
    if let Err(e) = fs::create_dir_all(&app_cache) {
        eprintln!("Warning: Could not create cache dir: {}", e);
        return false;
    }

    true
}
```

Treat cache as ephemeral. If the cache is gone, your app should rebuild it, not crash.

## Pitfalls and errors

Assuming `Some` is the most common mistake. If you use `.unwrap()` on `dirs::config_dir()`, your app crashes on systems where the path is undefined. This is rare but happens in containerized environments or minimal setups. Use `if let` or `?` with a custom error.

Permissions are another trap. `dirs` gives you the path, not permission to write. If the user has a weird setup where `~/.config` is read-only, `fs::create_dir_all` returns an `io::Error`. You must handle `io::ErrorKind::PermissionDenied`. Show a helpful message to the user. Don't just print a stack trace.

`PathBuf` ownership can confuse beginners. `dirs::config_dir()` returns `Option<PathBuf>`. This is an owned value. You can move it. If you need to use the path multiple times, clone it or borrow it. Cloning a `PathBuf` is cheap; it copies the string data, which is usually small.

```rust
use dirs;

fn main() {
    let config = dirs::config_dir();

    // First use: borrow.
    if let Some(path) = &config {
        println!("Path: {}", path.display());
    }

    // Second use: borrow again.
    // No need to clone because we borrowed with &config.
    if let Some(path) = &config {
        println!("Length: {}", path.to_string_lossy().len());
    }
}
```

If you move the `PathBuf` out of the `Option` once, you can't use it again. The compiler rejects this with `E0382` (use of moved value). Borrow with `&` to avoid moving.

Treat `io::Error` as a conversation with the user, not a bug in your code. Permissions and disk space are external factors.

## When to use dirs and when to look elsewhere

Use `dirs` when you need a lightweight, zero-dependency way to get standard paths for config, data, cache, and executables. It covers the basics for 95% of CLI tools and desktop apps.

Use `directories` when you need more granular control over subdirectories or require support for less common platforms. It builds on `dirs` but offers a project-based API that handles app-specific subfolders more explicitly.

Use `xdg-home` when you are building a Linux-only tool and want strict adherence to the XDG specification without Windows or macOS fallbacks.

Use manual environment variable parsing when you need to support custom paths defined by your own application, like `MY_APP_HOME`, and want to override the system defaults entirely.

Reach for `std::env::temp_dir()` when you need a temporary location that the OS will clean up automatically. Do not use `dirs::cache_dir()` for temporary files that should disappear after the run.

Pick the crate that matches your portability needs. `dirs` is the default choice for a reason.

## Where to go next

- [How to create temporary files](/en/how-to-create-temporary-files/)
- [How to read a file](/en/how-to-read-a-file/)
- [How to Watch for File Changes in Rust (notify crate)](/en/how-to-watch-for-file-changes-in-rust-notify-crate/)