directories_next

Struct BaseDirs

Source
pub struct BaseDirs { /* private fields */ }
Expand description

BaseDirs provides paths of user-invisible standard directories, following the conventions of the operating system the library is running on.

To compute the location of cache, config or data directories for individual projects or applications, use ProjectDirs instead.

§Examples

All examples on this page are computed with a user named Alice.

use directories_next::BaseDirs;
if let Some(base_dirs) = BaseDirs::new() {
    base_dirs.config_dir();
    // Linux:   /home/alice/.config
    // Windows: C:\Users\Alice\AppData\Roaming
    // macOS:   /Users/Alice/Library/Application Support
}

Implementations§

Source§

impl BaseDirs

Source

pub fn new() -> Option<BaseDirs>

Creates a BaseDirs struct which holds the paths to user-invisible directories for cache, config, etc. data on the system.

The returned value depends on the operating system and is either

  • Some, containing a snapshot of the state of the system’s paths at the time new() was invoked, or
  • None, if no valid home directory path could be retrieved from the operating system.

To determine whether a system provides a valid $HOME path, the following rules are applied:

§Linux and macOS:
  • Use $HOME if it is set and not empty.
  • If $HOME is not set or empty, then the function getpwuid_r is used to determine the home directory of the current user.
  • If getpwuid_r lacks an entry for the current user id or the home directory field is empty, then the function returns None.
§Windows:
  • Retrieve the user profile folder using SHGetKnownFolderPath.
  • If this fails, then the function returns None.

Note: This logic differs from std::env::home_dir, which works incorrectly on Linux, macOS and Windows.

Source

pub fn home_dir(&self) -> &Path

Returns the path to the user’s home directory.

PlatformValueExample
Linux$HOME/home/alice
macOS$HOME/Users/Alice
Windows{FOLDERID_Profile}C:\Users\Alice
Source

pub fn cache_dir(&self) -> &Path

Returns the path to the user’s cache directory.

PlatformValueExample
Linux$XDG_CACHE_HOME or $HOME/.cache/home/alice/.cache
macOS$HOME/Library/Caches/Users/Alice/Library/Caches
Windows{FOLDERID_LocalAppData}C:\Users\Alice\AppData\Local
Source

pub fn config_dir(&self) -> &Path

Returns the path to the user’s config directory.

PlatformValueExample
Linux$XDG_CONFIG_HOME or $HOME/.config/home/alice/.config
macOS$HOME/Library/Application Support/Users/Alice/Library/Application Support
Windows{FOLDERID_RoamingAppData}C:\Users\Alice\AppData\Roaming
Source

pub fn data_dir(&self) -> &Path

Returns the path to the user’s data directory.

PlatformValueExample
Linux$XDG_DATA_HOME or $HOME/.local/share/home/alice/.local/share
macOS$HOME/Library/Application Support/Users/Alice/Library/Application Support
Windows{FOLDERID_RoamingAppData}C:\Users\Alice\AppData\Roaming
Source

pub fn data_local_dir(&self) -> &Path

Returns the path to the user’s local data directory.

PlatformValueExample
Linux$XDG_DATA_HOME or $HOME/.local/share/home/alice/.local/share
macOS$HOME/Library/Application Support/Users/Alice/Library/Application Support
Windows{FOLDERID_LocalAppData}C:\Users\Alice\AppData\Local
Source

pub fn executable_dir(&self) -> Option<&Path>

Returns the path to the user’s executable directory.

PlatformValueExample
Linux$XDG_BIN_HOME or $XDG_DATA_HOME/../bin or $HOME/.local/bin/home/alice/.local/bin
macOS
Windows
Source

pub fn runtime_dir(&self) -> Option<&Path>

Returns the path to the user’s runtime directory.

PlatformValueExample
Linux$XDG_RUNTIME_DIR/run/user/1001/
macOS
Windows

Trait Implementations§

Source§

impl Clone for BaseDirs

Source§

fn clone(&self) -> BaseDirs

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for BaseDirs

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.