Thanks to visit codestin.com
Credit goes to lib.rs

#specification #path #base-directory #xdg #context #env-var

bin+lib xdgdir

Resolves paths according to the XDG Base Directory Specification

1 unstable release

Uses new Rust 2024

0.8.0 Sep 15, 2025

#1153 in Filesystem

MIT license

20KB
370 lines

xdgdir

xdgdir helps you to resolves paths according to the XDG Base Directory Specification.

  • Zero I/O: The library performs no filesystem operations. It is a pure path resolver, making it fast, predictable, and suitable for any context, including async runtimes.
  • Spec Compliant: Correctly handles environment variables, empty variables, and default fallbacks as defined by the spec.
  • Simple API: Provides a minimal, ergonomic API for the most common use cases.

Getting started

Add xdgdir to your project's dependencies:

cargo add xdgdir

Usage

To get the set of directories for your specific application, use BaseDir::new().

use xdgdir::BaseDir;

fn main() {
    let dirs = BaseDir::new("my-app").unwrap();

    println!("Config file should be in: {}", dirs.config.display());
    // -> /home/user/.config/my-app
    println!("Data files should be in: {}", dirs.data.display());
    // -> /home/user/.local/share/my-app
}

To get the raw, non-application-specific base directories, use BaseDir::global().

use xdgdir::BaseDir;

fn main() {
    let global_dirs = BaseDir::global().unwrap();

    println!("Default user config dir: {}", global_dirs.config.display());
    // -> /home/user/.config
    println!("User executables dir: {}", global_dirs.bin.display());
    // -> /home/user/.local/bin
}

License

This project is licensed under the MIT License.

No runtime deps