Struct Dir

struct Dir { ... }

An object providing access to a directory on the filesystem.

Directories are automatically closed when they go out of scope. Errors detected on closing are ignored by the implementation of Drop.

Platform-specific behavior

On supported systems (including Windows and some UNIX-based OSes), this function acquires a handle/file descriptor for the directory. This allows functions like Dir::open_file to avoid TOCTOU errors when the directory itself is being moved.

On other systems, it stores an absolute path (see [canonicalize()]). In the latter case, no TOCTOU guarantees are made.

Examples

Opens a directory and then a file inside it.

#![feature(dirfd)]
use std::{fs::Dir, io};

fn main() -> std::io::Result<()> {
    let dir = Dir::open("foo")?;
    let mut file = dir.open_file("bar.txt")?;
    let contents = io::read_to_string(file)?;
    assert_eq!(contents, "Hello, world!");
    Ok(())
}

Implementations

impl Dir

fn open<P: AsRef<Path>>(path: P) -> io::Result<Self>

Attempts to open a directory at path in read-only mode.

Errors

This function will return an error if path does not point to an existing directory. Other errors may also be returned according to OpenOptions::open.

Examples

#![feature(dirfd)]
use std::{fs::Dir, io};

fn main() -> std::io::Result<()> {
    let dir = Dir::open("foo")?;
    let mut f = dir.open_file("bar.txt")?;
    let contents = io::read_to_string(f)?;
    assert_eq!(contents, "Hello, world!");
    Ok(())
}
fn open_file<P: AsRef<Path>>(self: &Self, path: P) -> io::Result<File>

Attempts to open a file in read-only mode relative to this directory.

Errors

This function will return an error if path does not point to an existing file. Other errors may also be returned according to OpenOptions::open.

Examples

#![feature(dirfd)]
use std::{fs::Dir, io};

fn main() -> std::io::Result<()> {
    let dir = Dir::open("foo")?;
    let mut f = dir.open_file("bar.txt")?;
    let contents = io::read_to_string(f)?;
    assert_eq!(contents, "Hello, world!");
    Ok(())
}

impl AsFd for fs::Dir

fn as_fd(self: &Self) -> BorrowedFd<'_>

impl AsRawFd for fs::Dir

fn as_raw_fd(self: &Self) -> RawFd

impl Debug for Dir

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

impl Freeze for Dir

impl From for fs::Dir

fn from(value: OwnedFd) -> Self

impl FromRawFd for fs::Dir

unsafe fn from_raw_fd(fd: RawFd) -> Self

impl IntoRawFd for fs::Dir

fn into_raw_fd(self: Self) -> RawFd

impl RefUnwindSafe for Dir

impl Send for Dir

impl Sync for Dir

impl Unpin for Dir

impl UnwindSafe for Dir

impl<T> Any for Dir

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for Dir

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for Dir

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

impl<T> From for Dir

fn from(t: T) -> T

Returns the argument unchanged.

impl<T, U> Into for Dir

fn into(self: Self) -> U

Calls U::from(self).

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

impl<T, U> TryFrom for Dir

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

impl<T, U> TryInto for Dir

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